yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

Extra commas from inline R code #2279

Closed kendalorgera closed 10 months ago

kendalorgera commented 11 months ago

After posting to stackoverflow, I was recommended to reach out to you regarding this issue I'm facing. Using R Markdown, I created hundreds of simple frequency tables, but once I knit, the output always puts commas following each table. I personally can't tell if it's a knitr issue or something with apply or a bug for something else...but would appreciate any insight here.

Reproducible example here:

---
output: pdf_document
---

```{r gitbug, include=FALSE, warning=FALSE}
knitr::opts_chunk$set( echo = FALSE , warning = FALSE , message = FALSE , cache = FALSE )

# Load packages
library( ggplot2 )
library( knitr )
library( data.table )
library( kableExtra )

# Recreate comma issue after sapply 

cols <- list( diamonds )
dfs <- list( diamonds )
jan <- data.table( diamonds ) 
cols_jan <- colnames( diamonds[ , c( 1:4 , 7 ) ])

tabs <- function( number , design , dts ){

          x <- y <- z <- NULL 
          dts <- jan
          vars <- cols_jan[number]

          out <- x <- y <- z <- NULL 
          x <- dts[ , .( counts = .N ) , by= vars ]
          x <- x[ order( x[ , 1 ] ) ,  ]
          x[ , `:=` ( Percent = ifelse( counts < 30 , NA , counts/nrow( dts ))) , ]
          row.names( x ) <- NULL 
          x[ , counts := ifelse( counts < 30 , NA , as.numeric( counts )) , ]

          z <- x 
          z[ , `:=` (
                  counts = scales::number( counts , accuracy = 1 , big.mark="," ) ,
                  Percent = scales::percent( Percent , accuracy = 0.1 ) 
          )]  

          colnames( z ) <- c( ' ' , 'Counts' , 'Frequency' )
          out <- knitr::kable( z , format = 'latex', booktabs = TRUE )
          out

          }
r sapply( 2:4 , tabs )

Output creates all of the tables I need, but adds a comma in between each table despite multiple efforts to fix the issue. If you visit my stackoverflow question, you'll see an example screenshot of the commas appearing below each table it outputs. https://stackoverflow.com/questions/75338149/remove-comma-following-apply-statement-and-function-in-r

I've also run this on multiple versions of R with and without updates to knitr, but to no avail.

Session info: R version 4.2.2 (2022-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044), RStudio 2023.6.1.524

Locale: LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 LC_NUMERIC=C LC_TIME=English_United States.utf8

Package version: evaluate_0.17 glue_1.6.2 graphics_4.2.2 grDevices_4.2.2 highr_0.9 knitr_1.40 magrittr_2.0.3 methods_4.2.2 stats_4.2.2
stringi_1.7.8 stringr_1.4.1 tools_4.2.2 utils_4.2.2 xfun_0.33 yaml_2.3.5


By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

yihui commented 10 months ago

I just posted an answer on SO: https://stackoverflow.com/a/76917098/559676 Thanks!

github-actions[bot] commented 4 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.