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 %>%
  tbl_summary(by = trt) %>%
  bold_levels() %>%
  italicize_labels() %>%
  as_kable()
Characteristic Drug A, N = 98 Drug B, N = 102
Age, yrs 46 (37, 59) 48 (39, 56)
Unknown 7 4
Marker Level, ng/mL 0.84 (0.24, 1.57) 0.52 (0.19, 1.20)
Unknown 6 4
T Stage
T1 28 (29%) 25 (25%)
T2 25 (26%) 29 (28%)
T3 22 (22%) 21 (21%)
T4 23 (23%) 27 (26%)
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
Patient Died 52 (53%) 60 (59%)
Months to Death/Censor 23.5 (17.4, 24.0) 21.2 (14.6, 24.0)
patient_characteristics <-
  trial %>%
  select(trt, age, grade, response) %>%
  tbl_summary(by = trt)  
patient_characteristics
Characteristic Drug A, N = 981 Drug B, N = 1021
Age, yrs 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 Statistics presented: median (IQR); n (%)

With HTML ouput, you can include complex tables with footnotes, indendtation, and spanning table headers.

# Side-by-side Regression Models
# logistic regresssion 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, yrs 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.