Learn R Programming

⚠️There's a newer version (1.6.3) of this package.Take me there.

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.

  • Summarize data frames or tibbles easily in R. Perfect for presenting descriptive statistics, comparing group demographics (e.g creating a Table 1 for medical journals), and more. Automatically detects continuous, categorical, and dichotomous variables in your data set, calculates appropriate descriptive statistics, and also includes amount of missingness in each variable.

  • Summarize regression models in R and include reference rows for categorical variables. Common regression models, such as logistic regression and Cox proportional hazards regression, are automatically identified and the tables are pre-filled with appropriate column headers (i.e. Odds Ratio and Hazard Ratio).

  • Customize gtsummary tables using a growing list of formatting/styling functions. Bold labels, italicize levels, add p-value to summary tables, style the statistics however you choose, merge or stack tables to present results side by side… there are so many possibilities to create the table of your dreams!

  • Report statistics inline from summary tables and regression summary tables in R markdown. Make your reports completely reproducible!

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.

Example basic table:

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

# 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.

Save Individual Tables

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

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

For a Word file, use

tbl %>%
  as_flex_table() %>%
  flextable::save_as_docx()

Additional Resources

Cite gtsummary

> 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!
@ablack3, @ABorakati, @aghaynes, @ahinton-mmc, @aito123, @akarsteve, @akefley, @albertostefanelli, @alexis-catherine, @amygimma, @anaavu, @andrader, @angelgar, @arbet003, @arnmayer, @aspina7, @asshah4, @awcm0n, @barthelmes, @bcjaeger, @BeauMeche, @benediktclaus, @berg-michael, @bhattmaulik, @BioYork, @brachem-christian, @bwiernik, @bx259, @calebasaraba, @CarolineXGao, @ChongTienGoh, @Chris-M-P, @chrisleitzinger, @cjprobst, @clmawhorter, @CodieMonster, @coeus-analytics, @coreysparks, @ctlamb, @davidgohel, @davidkane9, @dax44, @dchiu911, @ddsjoberg, @DeFilippis, @denis-or, @dereksonderegger, @dieuv0, @discoleo, @djbirke, @dmenne, @edrill, @ElfatihHasabo, @emilyvertosick, @ercbk, @erikvona, @eweisbrod, @feizhadj, @fh-jsnider, @ge-generation, @Generalized, @ghost, @gjones1219, @gorkang, @GuiMarthe, @hass91, @HichemLa, @huftis, @hughjonesd, @iaingallagher, @ilyamusabirov, @IndrajeetPatil, @IsadoraBM, @j-tamad, @jalavery, @jeanmanguy, @jemus42, @jenifav, @jennybc, @JeremyPasco, @JesseRop, @jflynn264, @jhelvy, @jjallaire, @jmbarajas, @jmbarbone, @JoanneF1229, @joelgautschi, @jojosgithub, @JonGretar, @jordan49er, @jthomasmock, @juseer, @jwilliman, @karissawhiting, @kendonB, @kmdono02, @kwakuduahc1, @lamhine, @larmarange, @leejasme, @loukesio, @lspeetluk, @ltin1214, @lucavd, @LuiNov, @maia-sh, @Marsus1972, @matthieu-faron, @mbac, @mdidish, @MelissaAssel, @michaelcurry1123, @mljaniczek, @moleps, @motocci, @msberends, @mvuorre, @myensr, @MyKo101, @oranwutang, @palantre, @parmsam, @Pascal-Schmidt, @pedersebastian, @perlatex, @philsf, @polc1410, @postgres-newbie, @proshano, @raphidoc, @RaviBot, @rich-iannone, @RiversPharmD, @rmgpanw, @roman2023, @ryzhu75, @sachijay, @saifelayan, @sammo3182, @sandhyapc, @sbalci, @sda030, @shannonpileggi, @shengchaohou, @ShixiangWang, @simonpcouch, @slb2240, @slobaugh, @spiralparagon, @StaffanBetner, @Stephonomon, @storopoli, @szimmer, @tamytsujimoto, @TarJae, @themichjam, @THIB20, @tibirkrajc, @tjmeyers, @tldrcharlene, @tormodb, @toshifumikuroda, @UAB-BST-680, @uakimix, @uriahf, @Valja64, @vvm02, @xkcococo, @yonicd, @yoursdearboy, @zabore, @zachariae, @zaddyzad, @zeyunlu, @zhengnow, @zlkrvsm, @zongell-star, and @Zoulf001

Copy Link

Version

Install

install.packages('gtsummary')

Monthly Downloads

37,731

Version

1.6.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Last Published

September 30th, 2022

Functions in gtsummary (1.6.2)

add_n_regression

Add N to regression table
add_global_p

Add the global p-values
add_nevent

Add number of events to a regression table
add_ci

Add CI Column
add_n

Adds column with N to gtsummary table
add_p.tbl_continuous

P-values for tbl_continuous
add_p.tbl_cross

Adds p-value to crosstab table
add_p.tbl_summary

Adds p-values to summary tables
add_nevent_regression

Add event N to regression table
add_overall

Add column with overall summary statistics
add_q

Add a column of q-values to account for multiple comparisons
add_significance_stars

Add significance stars
add_p.tbl_svysummary

Adds p-values to svysummary tables
add_p.tbl_survfit

Adds p-value to survfit table
add_p

Adds p-values to gtsummary table
add_stat_label

Add statistic labels
add_stat

Add a custom statistic column
as_kable

Convert gtsummary object to a kable object
as_gt

Convert gtsummary object to a gt object
as_hux_table

Convert gtsummary object to a huxtable object
as_kable_extra

Convert gtsummary object to a kableExtra object
bold_p

Bold significant p-values or q-values
combine_terms

Combine terms in a regression model
.table_styling_expr_to_row_number

Object Convert Helper
bold_italicize_labels_levels

Bold or Italicize labels or levels in gtsummary tables
as_tibble.gtsummary

Convert gtsummary object to a tibble
add_vif

Add Variance Inflation Factor
as_flex_table

Convert gtsummary object to a flextable object
inline_text

Report statistics from gtsummary tables inline
inline_text.gtsummary

Report statistics from summary tables inline
deprecated

Deprecated functions
.create_gtsummary_object

Function uses table_body to create a gtsummary object
custom_tidiers

Collection of custom tidiers
continuous_summary

Summarize a continuous variable
inline_text.tbl_summary

Report statistics from summary tables inline
gtsummary-package

gtsummary: Presentation-Ready Data Summary and Analytic Result Tables
inline_text.tbl_survfit

Report statistics from survfit tables inline
inline_text.tbl_cross

Report statistics from cross table inline
inline_text.tbl_regression

Report statistics from regression summary tables inline
inline_text.tbl_uvregression

Report statistics from regression summary tables inline
inline_text.tbl_survival

Report statistics from survival summary tables inline
modify_column_alignment

Modify Column Alignment
ratio_summary

Summarize the ratio of two variables
proportion_summary

Summarize a proportion
modify_column_hide

Modify Hidden Columns
modify_column_indent

Add/Remove Indentation
kableExtra_utils

Special Character Escape
modify

Modify column headers, footnotes, spanning headers, and table captions
modify_column_merge

Modify Column Merging
plot

Plot Regression Coefficients
remove_row_type

Remove rows by type
separate_p_footnotes

Create footnotes for individual p-values
select_helpers

Select helper functions
reexports

Objects exported from other packages
style_ratio

Style significant figure-like rounding for ratios
style_pvalue

Style p-values
modify_table_header

Modify table_header
print_gtsummary

print and knit_print methods for gtsummary objects
modify_table_styling

Modify Table Styling
style_number

Style numbers
modify_fmt_fun

Modify Formatting Functions
modify_table_body

Modify Table Body
set_gtsummary_theme

Set a gtsummary theme
sort_filter_p

Sort and filter variables in table by p-values
tbl_regression_methods

Methods for tbl_regression
tbl_cross

Create a cross table of summary statistics
tbl_continuous

Summarize a continuous variable
tbl_merge

Merge two or more gtsummary objects
tbl_butcher

Reduce size of gtsummary objects
style_percent

Style percentages
style_sigfig

Style significant figure-like rounding
syntax

Syntax and Notation
tbl_custom_summary

Create a table of summary statistics using a custom summary function
tbl_regression

Display regression model results in table
tbl_split

Split gtsummary table
tbl_survival

Creates table of univariate summary statistics for time-to-event endpoints
tbl_svysummary

Create a table of summary statistics from a survey object
tbl_uvregression

Display univariate regression model results in table
theme_gtsummary

Available gtsummary themes
tbl_survfit

Creates table of survival probabilities
tests

Tests/methods available in add_p() and add_difference()
tbl_survfit_errors

Common Sources of Error with tbl_survfit()
tbl_summary

Create a table of summary statistics
tbl_strata

Stratified gtsummary tables
trial

Results from a simulated study of two chemotherapy agents
tbl_stack

Stacks two or more gtsummary objects
add_n.tbl_summary

Add column with N
add_n.tbl_survfit

Add column with number of observations
add_nevent.tbl_survfit

Add column with number of observed events
add_difference

Add difference between groups
add_glance

Add Model Statistics