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 RStudio, and {gtsummary} tables are printed using {gt} when possible. Currently, {gt} supports HTML output, with LaTeX and RTF planned for the future.

trial %>%
  select(age, grade) %>%
  tbl_summary() %>%
  modify_table_styling(
    columns = label,
    rows = variable == "age" & row_type != "label",
    text_format = "indent2"
  ) %>%
  as_hux_table()
CharacteristicN = 200
Age47 (38, 57)
Unknown11
Grade
I68 (34%)
II68 (34%)
III64 (32%)
Median (IQR); n (%)
patient_characteristics <-
  trial %>%
  select(trt, age, grade, response) %>%
  tbl_summary(by = trt)  
patient_characteristics
Characteristic Drug A, N = 981 Drug B, N = 1021
Age 46 (37, 59) 48 (39, 56)
Unknown 7 4
Grade
I 35 (36%) 33 (32%)
II 32 (33%) 36 (35%)
III 31 (32%) 33 (32%)
Tumor Response 28 (29%) 33 (34%)
Unknown 3 4

1 Median (IQR); n (%)

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**")
)
Characteristic Tumor Response Time to Death
OR1 95% CI1 p-value HR1 95% CI1 p-value
Chemotherapy Treatment
Drug A
Drug B 1.13 0.60, 2.13 0.7 1.30 0.88, 1.92 0.2
Grade
I
II 0.85 0.39, 1.85 0.7 1.21 0.73, 1.99 0.5
III 1.01 0.47, 2.15 >0.9 1.79 1.12, 2.86 0.014
Age 1.02 1.00, 1.04 0.10 1.01 0.99, 1.02 0.3

1 OR = Odds Ratio, CI = Confidence Interval, HR = Hazard Ratio

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, 31 (32%) had grade III tumors.

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