kableExtra
kableExtra copied to clipboard
`collapse_rows` does not work with col.names set to empty string
Describe the bug
collapse_rows
does not collapse rows if the column name is set to empty string (via col.names
in kable()
). This used to work in <1.4 versions as I recall.
To Reproduce
library(knitr)
library(kableExtra)
df = structure(list(rn = c("n", "Age", "Age", "Measure", "Measure", "Cost",
"Cost"), rn2 = c("n", "(mean (SD))", "(median [IQR])",
"(mean (SD))", "(median [IQR])", "(mean (SD))", "(median [IQR])"
), Overall = c(" 4090", " 60.65 (10.96)", " 59.00 [51.00, 65.00]",
" 7.12 (1.48)", " 6.90 [6.20, 7.80]", "17411.75 (41009.85)",
"4793.42 [2013.08, 14398.02]")), row.names = c(NA, -7L), class = "data.frame")
x <- knitr::kable(df, "html")
collapse_rows(x, 1L)
rn | rn2 | Overall |
---|---|---|
n | n | 4090 |
Age | (mean (SD)) | 60.65 (10.96) |
(median [IQR]) | 59.00 [51.00, 65.00] | |
Measure | (mean (SD)) | 7.12 (1.48) |
(median [IQR]) | 6.90 [6.20, 7.80] | |
Cost | (mean (SD)) | 17411.75 (41009.85) |
(median [IQR]) | 4793.42 [2013.08, 14398.02] |
x1 <- knitr::kable(df, "html", col.names = c("", "", "Overall"))
collapse_rows(x1, 1L)
Overall | ||
---|---|---|
n | n | 4090 |
Age | (mean (SD)) | 60.65 (10.96) |
Age | (median [IQR]) | 59.00 [51.00, 65.00] |
Measure | (mean (SD)) | 7.12 (1.48) |
Measure | (median [IQR]) | 6.90 [6.20, 7.80] |
Cost | (mean (SD)) | 17411.75 (41009.85) |
Cost | (median [IQR]) | 4793.42 [2013.08, 14398.02] |
> sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux 8.9 (Ootpa)
Matrix products: default
BLAS: /opt/R/4.3.0/lib64/R/lib/libRblas.so
LAPACK: /opt/R/4.3.0/lib64/R/lib/libRlapack.so; LAPACK version 3.11.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
time zone: Europe/Amsterdam
tzcode source: system (glibc)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] kableExtra_1.4.0.3 knitr_1.45
loaded via a namespace (and not attached):
[1] svglite_2.1.3 cli_3.6.2 rlang_1.1.3 xfun_0.42 stringi_1.8.3 highr_0.10 jsonlite_1.8.8 glue_1.7.0 colorspace_2.1-0
[10] htmltools_0.5.7 sass_0.4.8 scales_1.3.0 rmarkdown_2.25 evaluate_0.23 munsell_0.5.0 jquerylib_0.1.4 fastmap_1.1.1 lifecycle_1.0.4
[19] stringr_1.5.1 compiler_4.3.0 rstudioapi_0.15.0 systemfonts_1.0.5 digest_0.6.34 viridisLite_0.4.2 R6_2.5.1 magrittr_2.0.3 bslib_0.6.1
[28] tools_4.3.0 cachem_1.0.8 xml2_1.3.6
I've come across the same issue, in the stackoverflow they gave me a workaround. Use the unicode character for "NO-BREAK SPACE" - "\U00A0"
This works:
x2 <- knitr::kable(df, "html", col.names = c("\U00A0", "\U00A0", "Overall"))
collapse_rows(x2, 1L)
Here's the link for where I got the answer: https://stackoverflow.com/questions/78326899/how-to-remove-the-name-of-first-column-of-a-table-with-collapse-rows-with-rmarkd/78327214