2

What is the best option or suggested workflow for adding captions to gtsummary tables in Rmarkdown documents?

For examples, I know there's a function for kable tables, so in theory one might convert one into the other using gtsummary::as_kable(), but somehow this doesn't feel optimal. Suggestions?

Thanks!

Marco B
  • 121
  • 6

1 Answers1

0

The gt package has many many functions for controlling how a gt table prints. I suggest you convert the gtsummary object to gt using the as_gt() function, then continue customizing with gt functions. https://gt.rstudio.com/ Small example included below. enter image description here

library(gtsummary)

mod <- glm(response ~ age + grade, trial, family = binomial)

tbl_regression(mod, exponentiate = TRUE) %>% 
  as_gt() %>%
  gt::tab_header("Table 1. Very Informative Model",
                 subtitle = "Highly Confidential")

enter image description here

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • Thanks for your input, Daniel. I just noticed a problem with superscript/subscript. As you mentioned in this [answer](https://stackoverflow.com/questions/60534214/how-do-i-add-subscripts-to-labels-in-tables-using-the-gtsummary-package-in-r), converting to a `kable` is one option to get variable names such as `BMI (m·kg-2)`. – Marco B Nov 01 '20 at 10:31
  • Sorry, messed up with the comment: Thanks for your input, Daniel! I'm only having one problem with this approach: HTML/Markdown variable labels are not interpreted by `gt` when piping the table to `as_gt()`. In theory, `gt` should support HTML and Markdown in cell text. Would it be possible to make markup work in `gtsummary` cells (specifically: "row labels" in the `gt` diagram you pasted)? Or maybe I'm getting it wrong? I'm trying: `label = list( bmi ~ "BMI (kg · m-2" )` It already seems to work when piping `gtsummary` objects to `gtsummary::modify_*` functions? – Marco B Nov 01 '20 at 10:53
  • HI @MarcoB ! I can look into this further. But this seems to be a separate question from this post. Can you please post a new question with a reprex? (Use the gtsummary tag, and I'll be notified of the post.) – Daniel D. Sjoberg Nov 01 '20 at 13:58