yihui / knitr

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

feature: `kable()` white space trimming control #2066

Open jmbarbone opened 2 years ago

jmbarbone commented 2 years ago

Currently left and right whitespace is removed from the entire table. For outputs that utilize white space (e.g., left white space with monospace fonts) this causes problems with number alignments.

https://github.com/yihui/knitr/blob/b09cbc151a5531daec2a72eadaefdd565b633646/R/table.R#L151

An option to

library(knitr)
options(knitr.table.format = "pipe")
#> Error in option(knitr.table.format = "markdown"): could not find function "option"
x <- data.frame(a = c("15 (50)", " 1 ( 2)"))
kable(x)
| a       |
|:--------|
| 15 (50) |
| 1 ( 2)  |

Would prefer controls for:

# not implemented
kable(x, trim = "right")
 |a       |
 |:-------|
 |15 (50) |
 | 1 ( 2) |

Currently have to assign some arbitrary symbols that are then removed to preserve leading white space

x$a <- paste0("\\", x$a)
out <- kable(x)
out
|a        |
|:--------|
|\15 (50) |
|\ 1 ( 2) |
# not the prettiest
gsub("^[|]\\\\", "|", out)
|a        |
|:--------|
|15 (50) |
| 1 ( 2) |
xfun::session_info("knitr")
#> R version 4.1.1 (2021-08-10)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19043)
#> 
#> Locale:
#>   LC_COLLATE=English_United States.1252 
#>   LC_CTYPE=English_United States.1252   
#>   LC_MONETARY=English_United States.1252
#>   LC_NUMERIC=C                          
#>   LC_TIME=English_United States.1252    
#> 
#> Package version:
#>   evaluate_0.14   glue_1.4.2      graphics_4.1.1  grDevices_4.1.1
#>   highr_0.9       knitr_1.36      magrittr_2.0.1  methods_4.1.1  
#>   stats_4.1.1     stringi_1.7.5   stringr_1.4.0   tools_4.1.1    
#>   utils_4.1.1     xfun_0.26       yaml_2.2.1

Created on 2021-11-02 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> - Session info --------------------------------------------------------------- #> setting value #> version R version 4.1.1 (2021-08-10) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.1252 #> ctype English_United States.1252 #> tz America/New_York #> date 2021-11-02 #> #> - Packages ------------------------------------------------------------------- #> package * version date lib source #> backports 1.2.1 2020-12-09 [1] CRAN (R 4.1.0) #> cli 3.0.1 2021-07-17 [1] CRAN (R 4.1.0) #> crayon 1.4.1 2021-02-08 [1] CRAN (R 4.1.0) #> digest 0.6.28 2021-09-23 [1] CRAN (R 4.1.1) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.0) #> fansi 0.5.0 2021-05-25 [1] CRAN (R 4.1.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.0) #> fs 1.5.0 2020-07-31 [1] CRAN (R 4.1.0) #> glue 1.4.2 2020-08-27 [1] CRAN (R 4.1.0) #> highr 0.9 2021-04-16 [1] CRAN (R 4.1.0) #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.1) #> knitr * 1.36 2021-09-29 [1] CRAN (R 4.1.1) #> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.1) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.1.0) #> pillar 1.6.4 2021-10-18 [1] CRAN (R 4.1.1) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.0) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.1.0) #> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.1.0) #> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.1.0) #> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.1.0) #> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.1.1) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.0) #> rlang 0.4.11 2021-08-11 [1] Github (r-lib/rlang@4762538) #> rmarkdown 2.11 2021-09-14 [1] CRAN (R 4.1.0) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.0) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.1.0) #> stringi 1.7.5 2021-10-04 [1] CRAN (R 4.1.1) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.0) #> styler 1.6.2 2021-09-23 [1] CRAN (R 4.1.1) #> tibble 3.1.4 2021-08-25 [1] CRAN (R 4.1.1) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.0) #> vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.0) #> withr 2.4.2 2021-04-18 [1] CRAN (R 4.1.0) #> xfun 0.26 2021-09-14 [1] CRAN (R 4.1.1) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.0) #> #> [1] C:/Users/jbarbone/Documents/R/win-library/4.1 #> [2] C:/Program Files/R/R-4.1.1/library ```
yihui commented 2 years ago

White spaces in the Markdown source shouldn't matter when the table is rendered (e.g., to HTML/PDF). It seems that you want the Markdown source to be more human-readable. I think we can provide an option to preserve the white spaces.