Building Publication Tables with gtsummary: A Hands-on Course
From Table 1 to regression tables and Word export
Introduction
English version — machine-assisted translation (by generative AI, Anthropic Claude) of the original Japanese hands-on. 日本語版はこちら.
In this hands-on, the goal is to become able to build tables that can go straight into a paper, using R’s gtsummary package.
- Patient characteristics tables (the so-called Table 1)
- p-values for group comparisons, and confidence intervals for differences
- Stratified tables, and combining several tables
- Result tables for regression analyses (odds ratios, hazard ratios, etc.)
- Customization of headers, footnotes, bold text, journal formats, etc.
- Output to Word / HTML / images
- Forest plots of odds ratios and similar
Who this is for
- People who have read a
data.frameinto R or have seen|>(the pipe) - People who know terms like mean, median, proportion, t-test, and chi-squared test (around the level of an introductory statistics certificate)
A feature of this hands-on: R runs in your browser
The code cells in each chapter are executed by R inside your browser (a technology called webR). Edit the code and press “Run Code”, and the table is rebuilt immediately. No installation is needed.
- Right after opening each chapter, loading R and the packages takes several tens of seconds to about a minute
- Processing slows down if you move the tab to the background while loading, so please wait until it appears
- Viewing on a PC is recommended
Chapter overview
| Chapter | Contents |
|---|---|
| 1 | Your first table: the basics of tbl_summary() |
| 2 | Group comparisons and p-values: by, add_p(), and more |
| 3 | Stratifying and combining: tbl_strata(), tbl_merge(), tbl_stack() |
| 4 | Regression tables: tbl_regression(), tbl_uvregression() |
| 5 | Customizing the look: modify_*(), themes |
| 6 | Output and citing results in the text: Word / HTML / inline_text() |
| 7 | Plotting odds ratios (forest plots) |
| 8 | Exercises |
Practice data: trial
We use data from a fictional anticancer drug trial (200 people) that comes with gtsummary.
| Variable | Meaning |
|---|---|
trt |
Treatment group (Drug A / Drug B) |
age |
Age |
marker |
Tumor marker level (ng/mL) |
stage |
T stage (T1–T4) |
grade |
Grade (I–III) |
response |
Tumor response yes (1) / no (0) |
death |
Died (1) / alive (0) |
ttdeath |
Months to death or censoring |
Each variable has an English label (e.g. age → “Age”), and the tables display these labels.
Using it on your own PC
install.packages(c("gtsummary", "dplyr", "broom", "broom.helpers", "cardx",
"car", "parameters", "smd", "flextable", "ggplot2", "ggstats", "patchwork"))gtsummary uses the helper packages listed here for computing p-values and building regression tables. If you see a message like “package ‘broom’ is required”, install that package.
Some function names and arguments differ from 1.x (e.g. modify_footnote() → modify_footnote_header()). If code from an older article does not work, check your version: packageVersion("gtsummary")