knitr::opts_chunk$set(
  collapse = TRUE,
  warning = FALSE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

R-CMD-check CRAN status Codecov test coverage DOI:10.32614/RJ-2021-053

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)

# summarize the data with our package
table1 <- 
  trial %>%
  tbl_summary(include = c(age, grade, response))
# Had to manually save images in temp file, not sure if better way.
gt::gtsave(as_gt(table1), file = "man/figures/README-tbl_summary_print_simple-1.png")
knitr::include_graphics("man/figures/README-tbl_summary_print_simple-1.png")

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(
    trial,
    include = c(age, grade, response),
    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()
gt::gtsave(as_gt(table2), file = "man/figures/README-tbl_summary_print_extra-1.png")
knitr::include_graphics("man/figures/README-tbl_summary_print_extra-1.png")

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)
gt::gtsave(as_gt(t1), file = "man/figures/README-tbl_regression_printa-1.png")
knitr::include_graphics("man/figures/README-tbl_regression_printa-1.png")

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**")
  )
gt::gtsave(as_gt(tbl_merge_ex1), file = "man/figures/README-tbl_merge_ex1-1.png")
knitr::include_graphics("man/figures/README-tbl_merge_ex1-1.png")

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.

knitr::include_graphics("man/figures/gt_output_formats.PNG")

Save Individual Tables

{gtsummary} tables can also be saved directly to file as an image, HTML, Word, RTF, and LaTeX file.

tbl %>%
  as_gt() %>%
  gt::gtsave(filename = ".") # use extensions .png, .html, .docx, .rtf, .tex, .ltx

Additional Resources

Cite gtsummary

```{text, comment="", eval = FALSE}

citation("gtsummary")

To cite gtsummary in publications use:

Sjoberg DD, Whiting K, Curry M, Lavery JA, Larmarange J. Reproducible summary tables with the gtsummary package. The R Journal 2021;13:570–80. https://doi.org/10.32614/RJ-2021-053.

A BibTeX entry for LaTeX users is

@Article{gtsummary, author = {Daniel D. Sjoberg and Karissa Whiting and Michael Curry and Jessica A. Lavery and Joseph Larmarange}, title = {Reproducible Summary Tables with the gtsummary Package}, journal = {{The R Journal}}, year = {2021}, url = {https://doi.org/10.32614/RJ-2021-053}, doi = {10.32614/RJ-2021-053}, volume = {13}, issue = {1}, pages = {570-580}, } ```

Contributing

Big thank you to @jeffreybears for the hex sticker!

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. Thank you to all contributors!



ddsjoberg/gtsummary documentation built on Nov. 3, 2023, 11:42 a.m.