Codecov test coverage R build status CRAN status Lifecycle: maturing

gtsummary

The {gtsummary} package provides an elegant and flexible way to create publication-ready analytical and summary tables using the R programming language. The {gtsummary} package summarizes data sets, regression models, and more, using sensible defaults with highly customizable capabilities.

By leveraging {broom}, {gt}, and {labelled} packages, {gtsummary} creates beautifully formatted, ready-to-share summary and result tables in a single line of R code!

Check out the examples below, review the vignettes for a detailed exploration of the output options, and view the gallery for various customization examples.

Installation

The {gtsummary} package was written as a companion to the {gt} package from RStudio. You can install {gtsummary} with the following code.

install.packages("gtsummary")

Install the development version of {gtsummary} with:

remotes::install_github("ddsjoberg/gtsummary")

Examples

Summary Table

Use tbl_summary() to summarize a data frame.

animated

Example basic table:

library(gtsummary)
# make dataset with a few variables to summarize
trial2 <- trial %>% select(trt, age, grade, response)

# summarize the data with our package
table1 <- tbl_summary(trial2)

There are many customization options to add information (like comparing groups) and format results (like bold labels) in your table. See the tbl_summary() tutorial for many more options, or below for one example.

table2 <- 
  tbl_summary(
    trial2,
    by = trt, # split table by group
    missing = "no" # don't list missing data separately
  ) %>%
  add_n() %>% # add column with total number of non-missing observations
  add_p() %>% # test for a difference between groups
  modify_header(label = "**Variable**") %>% # update the column header
  bold_labels() 

Regression Models

Use tbl_regression() to easily and beautifully display regression model results in a table. See the tutorial for customization options.

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

t1 <- tbl_regression(mod1, exponentiate = TRUE)

Side-by-side Regression Models

You can also present side-by-side regression model results using tbl_merge()

library(survival)

# build survival model table
t2 <-
  coxph(Surv(ttdeath, death) ~ trt + grade + age, trial) %>%
  tbl_regression(exponentiate = TRUE)

# merge tables 
tbl_merge_ex1 <-
  tbl_merge(
    tbls = list(t1, t2),
    tab_spanner = c("**Tumor Response**", "**Time to Death**")
  )

Review even more output options in the table gallery.

gtsummary + R Markdown

The {gtsummary} package was written to be a companion to the {gt} package from RStudio. But not all output types are supported by the {gt} package. Therefore, we have made it possible to print {gtsummary} tables with various engines.

Review the gtsummary + R Markdown vignette for details.

Contributing

Please note that the {gtsummary} project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms. A big thank you to all contributors!
[@ablack3](https://github.com/ablack3), [@ahinton-mmc](https://github.com/ahinton-mmc), [@barthelmes](https://github.com/barthelmes), [@calebasaraba](https://github.com/calebasaraba), [@davidgohel](https://github.com/davidgohel), [@davidkane9](https://github.com/davidkane9), [@dax44](https://github.com/dax44), [@ddsjoberg](https://github.com/ddsjoberg), [@DeFilippis](https://github.com/DeFilippis), [@emilyvertosick](https://github.com/emilyvertosick), [@GuiMarthe](https://github.com/GuiMarthe), [@hughjonesd](https://github.com/hughjonesd), [@jeanmanguy](https://github.com/jeanmanguy), [@jennybc](https://github.com/jennybc), [@jflynn264](https://github.com/jflynn264), [@joelgautschi](https://github.com/joelgautschi), [@jwilliman](https://github.com/jwilliman), [@karissawhiting](https://github.com/karissawhiting), [@khizzr](https://github.com/khizzr), [@larmarange](https://github.com/larmarange), [@leejasme](https://github.com/leejasme), [@ltin1214](https://github.com/ltin1214), [@margarethannum](https://github.com/margarethannum), [@matthieu-faron](https://github.com/matthieu-faron), [@michaelcurry1123](https://github.com/michaelcurry1123), [@moleps](https://github.com/moleps), [@MyKo101](https://github.com/MyKo101), [@oranwutang](https://github.com/oranwutang), [@ryzhu75](https://github.com/ryzhu75), [@sammo3182](https://github.com/sammo3182), [@simonpcouch](https://github.com/simonpcouch), [@slb2240](https://github.com/slb2240), [@slobaugh](https://github.com/slobaugh), [@tormodb](https://github.com/tormodb), [@zabore](https://github.com/zabore), and [@zeyunlu](https://github.com/zeyunlu)