rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
2.04k stars 209 forks source link

`gt::as_latex` output error when working with spanning headers #1195

Open themichjam opened 1 year ago

themichjam commented 1 year ago

I know the below gt::as_latex works with tbl_summary, but when applied to tbl_merge objects the output its strange?

both reprex below, first is with gtsummary tbls and the second with just gt tbls, thanks!

library(gtsummary)
library(tidyverse)

# tbl_summary - this works
trial %>%
  select(age, trt) %>%
  tbl_summary() %>%
  as_gt() %>%
  gt::as_latex()

# tbl_summary() output
\captionsetup[table]{labelformat=empty,skip=1pt}
\begin{longtable}{lc}
\toprule
\textbf{Characteristic} & \textbf{N = 200}\textsuperscript{1} \\ 
\midrule
Age, yrs & 47 (38, 57) \\ 
Unknown & 11 \\ 
Chemotherapy Treatment &  \\ 
Drug A & 98 (49\%) \\ 
Drug B & 102 (51\%) \\ 
\bottomrule
\end{longtable}
\vspace{-5mm}
\begin{minipage}{\linewidth}
\textsuperscript{1}Statistics presented: median (IQR); n (\%) \\ 
\end{minipage}

# But when applied to tbl_merge it doesn't make sense?

t1 <-
  glm(response ~ trt + grade + age, trial, family = binomial) %>%
  tbl_regression(exponentiate = TRUE)
t2 <-
  coxph(Surv(ttdeath, death) ~ trt + grade + age, trial) %>%
  tbl_regression(exponentiate = TRUE)
tbl_merge_ex1 <-
  tbl_merge(
    tbls = list(t1, t2),
    tab_spanner = c("**Tumor Response**", "**Time to Death**")
  ) %>%
  as_gt() %>%
  gt::as_latex()
tbl_merge_ex1

# output
[1] "\\setlength{\\LTpost}{0mm}\n\\begin{longtable}{lcccccc}\n\\toprule\n & \\multicolumn{3}{c}{\\textbf{Tumor Response}} & \\multicolumn{3}{c}{\\textbf{Time to Death}} \\\\ \n\\cmidrule(lr){2-4} \\cmidrule(lr){5-7}\n\\textbf{Characteristic} & \\textbf{OR}\\textsuperscript{1} & \\textbf{95\\% CI}\\textsuperscript{1} & \\textbf{p-value} & \\textbf{HR}\\textsuperscript{1} & \\textbf{95\\% CI}\\textsuperscript{1} & \\textbf{p-value} \\\\ \n\\midrule\nChemotherapy Treatment &  &  &  &  &  &  \\\\ \n    Drug A & — & — &  & — & — &  \\\\ \n    Drug B & 1.13 & 0.60, 2.13 & 0.7 & 1.30 & 0.88, 1.92 & 0.2 \\\\ \nGrade &  &  &  &  &  &  \\\\ \n    I & — & — &  & — & — &  \\\\ \n    II & 0.85 & 0.39, 1.85 & 0.7 & 1.21 & 0.73, 1.99 & 0.5 \\\\ \n    III & 1.01 & 0.47, 2.15 & >0.9 & 1.79 & 1.12, 2.86 & 0.014 \\\\ \nAge & 1.02 & 1.00, 1.04 & 0.10 & 1.01 & 0.99, 1.02 & 0.3 \\\\ \n\\bottomrule\n\\end{longtable}\n\\begin{minipage}{\\linewidth}\n\\textsuperscript{1}OR = Odds Ratio, CI = Confidence Interval, HR = Hazard Ratio\\\\\n\\end{minipage}\n"

So after advice from gtsummary creators, it seems its an issue with gt and spanning headers, I tried just a gt tbl with spanning headers and got similar results:

# just gt tbl with spanning headers
gtcars %>%
  dplyr::select(
    -mfr, -trim, bdy_style,
    -drivetrain, -trsmn, -ctry_origin
  ) %>%
  dplyr::slice(1:8) %>%
  gt(rowname_col = "model") %>%
  tab_spanner(
    label = "performance",
    columns = c(
      hp, hp_rpm, trq, trq_rpm,
      mpg_c, mpg_h
    )
  ) %>%
  gt::as_latex()

# output
[1] "\\begin{longtable}{l|rlrrrrrrr}\n\\toprule\n\\multicolumn{1}{l}{} &  &  & \\multicolumn{6}{c}{performance} &  \\\\ \n\\cmidrule(lr){4-9}\n\\multicolumn{1}{l}{} & year & bdy\\_style & hp & hp\\_rpm & trq & trq\\_rpm & mpg\\_c & mpg\\_h & msrp \\\\ \n\\midrule\nGT & 2017 & coupe & 647 & 6250 & 550 & 5900 & 11 & 18 & 447000 \\\\ \n458 Speciale & 2015 & coupe & 597 & 9000 & 398 & 6000 & 13 & 17 & 291744 \\\\ \n458 Spider & 2015 & convertible & 562 & 9000 & 398 & 6000 & 13 & 17 & 263553 \\\\ \n458 Italia & 2014 & coupe & 562 & 9000 & 398 & 6000 & 13 & 17 & 233509 \\\\ \n488 GTB & 2016 & coupe & 661 & 8000 & 561 & 3000 & 15 & 22 & 245400 \\\\ \nCalifornia & 2015 & convertible & 553 & 7500 & 557 & 4750 & 16 & 23 & 198973 \\\\ \nGTC4Lusso & 2017 & coupe & 680 & 8250 & 514 & 5750 & 12 & 17 & 298000 \\\\ \nFF & 2015 & coupe & 652 & 8000 & 504 & 6000 & 11 & 16 & 295000 \\\\ \n\\bottomrule\n\\end{longtable}\n"
attr(,"class")
[1] "knit_asis"
attr(,"knit_meta")
attr(,"knit_meta")[[1]]
$name
[1] "amsmath"

$options
NULL

$extra_lines
NULL

attr(,"class")
[1] "latex_dependency"

attr(,"knit_meta")[[2]]
$name
[1] "booktabs"

$options
NULL

$extra_lines
NULL

attr(,"class")
[1] "latex_dependency"

attr(,"knit_meta")[[3]]
$name
[1] "caption"

$options
NULL

$extra_lines
NULL

attr(,"class")
[1] "latex_dependency"

attr(,"knit_meta")[[4]]
$name
[1] "longtable"

$options
NULL

$extra_lines
NULL

attr(,"class")
[1] "latex_dependency"

attr(,"knit_cacheable")
[1] NA
olivroy commented 2 months ago

This is still an issue with gt 0.11.9000. The last column doesn't have a border #1864

image