kableExtra
kableExtra copied to clipboard
group_rows_latex() can misplace 'new_rowtext'
Thanks again for this essential package! (kableExtra v 1.3.4)
I am using pack_rows()
to add row groupings to my table. Internally, it calls group_rows_latex()
. At line 73 (in my case, perhaps also 69?) group_rows_latex()
uses sub()
to replace rowtext
with new_rowtext
. However, the exact value of rowtext
also occurs earlier in character(out)
! so new_rowtext
gets placed there by accident (see below).
Before replacement:
\begin{tabular}[t]{lllll}
\toprule
\multicolumn{1}{c}{ } & \multicolumn{1}{c}{Placebo} & \multicolumn{1}{c}{TRT 10 mg} & \multicolumn{1}{c}{TRT 20 mg} & \multicolumn{1}{c}{ } \\
\cmidrule(l{3pt}r{3pt}){2-2} \cmidrule(l{3pt}r{3pt}){3-3} \cmidrule(l{3pt}r{3pt}){4-4}
& \makecell[c]{Placebo\\(N = 4)} & \makecell[c]{TRT Total\\(N = 4)} & \makecell[c]{TRT Total\\(N = 4)} & \makecell[c]{All\\(N = 12)}\\
\midrule
\addlinespace[0.3em]
\multicolumn{5}{l}{\textbf{Race}}\\
\hspace{1em}White & 0 & 0 & 0 & 0\\
\hspace{1em}Black & 0 & 0 & 0 & 0\\
\hspace{1em}Asian & 0 & 0 & 0 & 0\\
\hspace{1em}Other & 0 & 0 & 0 & 0\\
\hspace{1em}Missing & 0 & 0 & 0 & 0\\
n & 0 & 0 & 0 & 0\\
Mean (SD) & NaN (NA) & NaN (NA) & NaN (NA) & NaN (NA)\\
Median & NA & NA & NA & NA\\
Min, Max & Inf, -Inf & Inf, -Inf & Inf, -Inf & Inf, -Inf\\
\bottomrule
\end{tabular}
After replacement:
\begin{tabular}[t]{lllll}
\toprule
\multicolumn{1}{c}{ } & \multicolumn{1}{c}{Placebo} & \multicolumn{1}{c}{TRT 10 mg} & \multicolumn{1}{c}{TRT 20 mg} & \multicolumn{1}{c}{ } \\
\cmidrule(l{3pt}r{3pt}){2-2} \cmidrule(l{3pt}r{3pt}){3-3} \cmidrule(l{3pt}r{3pt}){4-4}
& \makecell[c]{Placebo\\(N = 4)} & \makecell[c]{TRT Total\\(N = 4)} & \makecell[c]{TRT Total\\(N = 4)} & \makecell[c]{All\\(N = 12)}\\
\midrule
\addlinespace[0.3em]
\multicolumn{5}{l}{\textbf{Race}}\\
\hspace{1em}White & 0 & 0 & 0 & 0\\
\hspace{1em}Black & 0 & 0 & 0 & 0\\
\hspace{1em}Asia\addlinespace[0.3em]
\multicolumn{5}{l}{\textbf{Body Mass Index}}\\
n & 0 & 0 & 0 & 0\\
\hspace{1em}Other & 0 & 0 & 0 & 0\\
\hspace{1em}Missing & 0 & 0 & 0 & 0\\
n & 0 & 0 & 0 & 0\\
Mean (SD) & NaN (NA) & NaN (NA) & NaN (NA) & NaN (NA)\\
Median & NA & NA & NA & NA\\
Min, Max & Inf, -Inf & Inf, -Inf & Inf, -Inf & Inf, -Inf\\
\bottomrule
\end{tabular}
Instead of
out <- sub(paste0(rowtext, "\\\\\\\\\n"), paste0(new_rowtext, "\\\\\\\\\n"), out)
maybe you want
out <- sub(paste0("\\\\\\\\\n","\nrowtext, "\\\\\\\\\n"), paste0("\\\\\\\\\n",new_rowtext, "\\\\\\\\\n"), out)
to search only at the beginnings of effective table records.
Is this still an issue? If so, could you post the input you used to reproduce it?
Thanks for the question. I hope to answer it soon.
On Mon, Dec 4, 2023 at 4:13 AM dmurdoch @.***> wrote:
Is this still an issue? If so, could you post the input you used to reproduce it?
— Reply to this email directly, view it on GitHub https://github.com/haozhu233/kableExtra/issues/703#issuecomment-1838125331, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABNMWIXUGPNEXYXE7DWK5D3YHWH2PAVCNFSM5TS3J662U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBTHAYTENJTGMYQ . You are receiving this because you authored the thread.Message ID: @.***>
Reproducible example: below, I want lines 6 through 9 to be grouped as "body mass index". But line 6 starts with "n", which is also found in the word "Asian", and the intervention is mistakenly placed there. Original post cites kableExtra 1.3.4, which is still the version on CRAN. See sessionInfo(). Thanks!
library(knitr)
library(kableExtra)
library(magrittr)
x <- data.frame(
statistic = c('White','Black','Asian','Other','Missing','n','Mean','Median','Min, Max'),
Placebo = c(0, 0, 0, 0, 0, 0, 'NaN', 'NA', 'Inf, -Inf'),
trt10mg = c(0, 0, 0, 0, 0, 0, 'NaN', 'NA', 'Inf, -Inf'),
trt20mg = c(0, 0, 0, 0, 0, 0, 'NaN', 'NA', 'Inf, -Inf'),
all = c(0, 0, 0, 0, 0, 0, 'NaN', 'NA', 'Inf, -Inf')
)
x %>% kable(format = 'latex') %>% pack_rows(group_label = 'body mass index', start_row = 6, end_row = 9) %>% writeLines
#>
#> \begin{tabular}{l|l|l|l|l}
#> \hline
#> statistic & Placebo & trt10mg & trt20mg & all\\
#> \hline
#> White & 0 & 0 & 0 & 0\\
#> \hline
#> Black & 0 & 0 & 0 & 0\\
#> \hline
#> Asia\multicolumn{5}{l}{\textbf{body mass index}}\\
#> \hline
#> \hspace{1em}n & 0 & 0 & 0 & 0\\
#> \hline
#> Other & 0 & 0 & 0 & 0\\
#> \hline
#> Missing & 0 & 0 & 0 & 0\\
#> \hline
#> n & 0 & 0 & 0 & 0\\
#> \hline
#> \hspace{1em}Mean & NaN & NaN & NaN & NaN\\
#> \hline
#> \hspace{1em}Median & NA & NA & NA & NA\\
#> \hline
#> \hspace{1em}Min, Max & Inf, -Inf & Inf, -Inf & Inf, -Inf & Inf, -Inf\\
#> \hline
#> \end{tabular}
Created on 2024-01-18 with reprex v2.0.2
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R Under development (unstable) (2023-10-04 r85267 ucrt)
#> os Windows 10 x64 (build 19044)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United States.utf8
#> ctype English_United States.utf8
#> tz America/New_York
#> date 2024-01-18
#> pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> cli 3.6.1 2023-03-23 [1] CRAN (R 4.4.0)
#> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.4.0)
#> digest 0.6.31 2022-12-11 [1] CRAN (R 4.4.0)
#> evaluate 0.21 2023-05-05 [1] CRAN (R 4.4.0)
#> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.4.0)
#> fs 1.6.2 2023-04-25 [1] CRAN (R 4.4.0)
#> glue 1.6.2 2022-02-24 [1] CRAN (R 4.4.0)
#> htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.4.0)
#> httr 1.4.5 2023-02-24 [1] CRAN (R 4.4.0)
#> kableExtra * 1.3.4 2021-02-20 [1] CRAN (R 4.4.0)
#> knitr * 1.42 2023-01-25 [1] CRAN (R 4.4.0)
#> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.4.0)
#> magrittr * 2.0.3 2022-03-30 [1] CRAN (R 4.4.0)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.4.0)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.4.0)
#> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.4.0)
#> rlang 1.1.0 2023-03-14 [1] CRAN (R 4.4.0)
#> rmarkdown 2.21 2023-03-26 [1] CRAN (R 4.4.0)
#> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.4.0)
#> rvest 1.0.3 2022-08-19 [1] CRAN (R 4.4.0)
#> scales 1.2.1 2022-08-20 [1] CRAN (R 4.4.0)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.4.0)
#> stringi 1.7.12 2023-01-11 [1] CRAN (R 4.4.0)
#> stringr 1.5.0 2022-12-02 [1] CRAN (R 4.4.0)
#> svglite 2.1.1 2023-01-10 [1] CRAN (R 4.4.0)
#> systemfonts 1.0.4 2022-02-11 [1] CRAN (R 4.4.0)
#> vctrs 0.6.2 2023-04-19 [1] CRAN (R 4.4.0)
#> viridisLite 0.4.2 2023-05-02 [1] CRAN (R 4.4.0)
#> webshot 0.5.4 2022-09-26 [1] CRAN (R 4.4.0)
#> withr 2.5.0 2022-03-03 [1] CRAN (R 4.4.0)
#> xfun 0.39 2023-04-20 [1] CRAN (R 4.4.0)
#> xml2 1.3.5 2023-07-06 [1] CRAN (R 4.4.0)
#> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.4.0)
#>
#> [1] C:/Users/tim.bergsma/AppData/Local/R/win-library/4.4
#> [2] C:/Program Files/R/R-devel/library
#>
#> ──────────────────────────────────────────────────────────────────────────────
I think the GitHub changes haven't made it to Cran yet. Could you try remotes::install_github("haozhu233/kableExtra")? I'm away from home now so I can't.
On Thu, Jan 18, 2024, 13:54 bergsmat @.***> wrote:
Reproducible example: below, I want lines 6 through 9 to be grouped as "body mass index". But line 6 starts with "n", which is also found in the word "Asian", and the intervention is mistakenly placed there. Original post cites kableExtra 1.3.4, which is still the version on CRAN. See sessionInfo(). Thanks!
library(knitr) library(kableExtra) library(magrittr) x <- data.frame( statistic = c('White','Black','Asian','Other','Missing','n','Mean','Median','Min, Max'), Placebo = c(0, 0, 0, 0, 0, 0, 'NaN', 'NA', 'Inf, -Inf'), trt10mg = c(0, 0, 0, 0, 0, 0, 'NaN', 'NA', 'Inf, -Inf'), trt20mg = c(0, 0, 0, 0, 0, 0, 'NaN', 'NA', 'Inf, -Inf'), all = c(0, 0, 0, 0, 0, 0, 'NaN', 'NA', 'Inf, -Inf') ) x %>% kable(format = 'latex') %>% pack_rows(group_label = 'body mass index', start_row = 6, end_row = 9) %>% writeLines#> #> \begin{tabular}{l|l|l|l|l}#> \hline#> statistic & Placebo & trt10mg & trt20mg & all\#> \hline#> White & 0 & 0 & 0 & 0\#> \hline#> Black & 0 & 0 & 0 & 0\#> \hline#> Asia\multicolumn{5}{l}{\textbf{body mass index}}\#> \hline#> \hspace{1em}n & 0 & 0 & 0 & 0\#> \hline#> Other & 0 & 0 & 0 & 0\#> \hline#> Missing & 0 & 0 & 0 & 0\#> \hline#> n & 0 & 0 & 0 & 0\#> \hline#> \hspace{1em}Mean & NaN & NaN & NaN & NaN\#> \hline#> \hspace{1em}Median & NA & NA & NA & NA\#> \hline#> \hspace{1em}Min, Max & Inf, -Inf & Inf, -Inf & Inf, -Inf & Inf, -Inf\#> \hline#> \end{tabular}
Created on 2024-01-18 with reprex v2.0.2 https://reprex.tidyverse.org Session info
sessioninfo::session_info()#> ─ Session info ───────────────────────────────────────────────────────────────#> setting value#> version R Under development (unstable) (2023-10-04 r85267 ucrt)#> os Windows 10 x64 (build 19044)#> system x86_64, mingw32#> ui RTerm#> language (EN)#> collate English_United States.utf8#> ctype English_United States.utf8#> tz America/New_York#> date 2024-01-18#> pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)#> #> ─ Packages ───────────────────────────────────────────────────────────────────#> package * version date (UTC) lib source#> cli 3.6.1 2023-03-23 [1] CRAN (R 4.4.0)#> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.4.0)#> digest 0.6.31 2022-12-11 [1] CRAN (R 4.4.0)#> evaluate 0.21 2023-05-05 [1] CRAN (R 4.4.0)#> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.4.0)#> fs 1.6.2 2023-04-25 [1] CRAN (R 4.4.0)#> glue 1.6.2 2022-02-24 [1] CRAN (R 4.4.0)#> htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.4.0)#> httr 1.4.5 2023-02-24 [1] CRAN (R 4.4.0)#> kableExtra * 1.3.4 2021-02-20 [1] CRAN (R 4.4.0)#> knitr * 1.42 2023-01-25 [1] CRAN (R 4.4.0)#> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.4.0)#> magrittr * 2.0.3 2022-03-30 [1] CRAN (R 4.4.0)#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.4.0)#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.4.0)#> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.4.0)#> rlang 1.1.0 2023-03-14 [1] CRAN (R 4.4.0)#> rmarkdown 2.21 2023-03-26 [1] CRAN (R 4.4.0)#> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.4.0)#> rvest 1.0.3 2022-08-19 [1] CRAN (R 4.4.0)#> scales 1.2.1 2022-08-20 [1] CRAN (R 4.4.0)#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.4.0)#> stringi 1.7.12 2023-01-11 [1] CRAN (R 4.4.0)#> stringr 1.5.0 2022-12-02 [1] CRAN (R 4.4.0)#> svglite 2.1.1 2023-01-10 [1] CRAN (R 4.4.0)#> systemfonts 1.0.4 2022-02-11 [1] CRAN (R 4.4.0)#> vctrs 0.6.2 2023-04-19 [1] CRAN (R 4.4.0)#> viridisLite 0.4.2 2023-05-02 [1] CRAN (R 4.4.0)#> webshot 0.5.4 2022-09-26 [1] CRAN (R 4.4.0)#> withr 2.5.0 2022-03-03 [1] CRAN (R 4.4.0)#> xfun 0.39 2023-04-20 [1] CRAN (R 4.4.0)#> xml2 1.3.5 2023-07-06 [1] CRAN (R 4.4.0)#> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.4.0)#> #> [1] C:/Users/tim.bergsma/AppData/Local/R/win-library/4.4#> [2] C:/Program Files/R/R-devel/library#> #> ──────────────────────────────────────────────────────────────────────────────
— Reply to this email directly, view it on GitHub https://github.com/haozhu233/kableExtra/issues/703#issuecomment-1899114480, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOYSQCM7TBYX5AJDJL25KTYPF4WVAVCNFSM5TS3J662U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBZHEYTCNBUHAYA . You are receiving this because you commented.Message ID: @.***>
I tested it last night and the bug is still there. I made one attempt to fix it but it didn't work out. I'll take a closer look later. @gfiumara thanks for the reproducible example. That makes things a lot easier!
@dmurdoch I submitted the current version to cran yesterday. I need to resolve a few reverse dependency problem before it's on cran.