knitr::opts_chunk$set(echo = TRUE, warning = FALSE)
library(gtsummary)
library(tidyverse)
library(survival)

gtsummary tables

Tables created with {gtsummary} can be integrated into R markdown documents. The {gtsummary} package was written to be a companion to the {gt} package from Posit.

patient_characteristics <-
  trial |> 
  tbl_summary(by = trt, include = c(age, grade, response))  
patient_characteristics

With HTML output, you can include complex tables with footnotes, indentation, and spanning table headers.

# Side-by-side Regression Models
# logistic regression model
t1 <-
  glm(response ~ trt + grade + age, trial, family = binomial) %>%
  tbl_regression(exponentiate = TRUE)
# time to death Cox model
t2 <-
  coxph(Surv(ttdeath, death) ~ trt + grade + age, trial) %>%
  tbl_regression(exponentiate = TRUE)

# printing merged table
tbl_merge(
  tbls = list(t1, t2),
  tab_spanner = c("**Tumor Response**", "**Time to Death**")
)

inline reporting

Any number/statistic from a {gtsummary} table can be reported inline in a R markdown document using the inline_text() function. See example below:

Among patients who received Drug A, r inline_text(patient_characteristics, variable = grade, level = "III", column = "Drug A") had grade III tumors.

For detailed examples using functions from {gtsummary}, visit the {gtsummary} website.



ddsjoberg/gtsummary documentation built on June 11, 2025, 10:29 p.m.