Install gtsummary
mainYou can install the stable version of gtsummary from CRAN using install.packages(). If you need the development version, use pak::pkg_install() pointing to the GitHub repository.
install.packages("gtsummary")repository·main·Indexed 22 days ago
https://github.com/ddsjoberg/gtsummaryAn R package for creating publication-ready analytical and summary tables. It provides tools like tbl_summary() for descriptive statistics, tbl_regression() for summarizing regression models, and tbl_merge() for presenting multiple models side-by-side. Tables can be exported to formats including .png, .html, .docx, .rtf, .tex, and .ltx via gt.
You can install the stable version of gtsummary from CRAN using install.packages(). If you need the development version, use pak::pkg_install() pointing to the GitHub repository.
install.packages("gtsummary")You can present multiple regression models side-by-side using tbl_merge(). This function takes a list of gtsummary tables and allows you to add a tab_spanner to group them under a common header.
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**")
)To save a table to a specific file format, convert it to a gt object using as_gt() and then use gt::gtsave(). Supported extensions include .png, .html, .docx, .rtf, .tex, and .ltx.
tbl |>
as_gt() |>
gt::gtsave(filename = ".") # use extensions .png, .html, .docx, .rtf, .tex, .ltxUse tbl_summary() to create descriptive statistics tables from data frames or tibbles. It automatically detects variable types (continuous, categorical, dichotomous) and includes missingness information. You can use the include argument to select specific variables and the by argument to split the table by a grouping variable.
library(gtsummary)
# Basic summary table
table1 <- trial |>
tbl_summary(include = c(age, grade, response))
# Customized summary table
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()Use tbl_regression() to display regression model results. It automatically identifies common models (like logistic or Cox proportional hazards) and pre-fills appropriate headers (e.g., Odds Ratio or Hazard Ratio). Use exponentiate = TRUE to display results as exponentiated coefficients.
mod1 <- glm(response ~ trt + age + grade, trial, family = binomial)
t1 <- tbl_regression(mod1, exponentiate = TRUE)