Post-process a table that was paginated with
gtsummary::tbl_split_by_rows() (via its variable_level argument). For each
page, a subtitle is built from the split level using a glue()
pattern, and the now-redundant split column is hidden.
This is useful for any split {gtsummary} table, such as listings
(tbl_listing()) split by treatment, or baseline/change tables
(tbl_baseline_chg()) and shift tables (tbl_shift()) split by PARAM.
The label is set both as a {gtsummary} caption (via
gtsummary::modify_caption()) and on the native variable_level attribute.
The caption makes the subtitle visible when the table is printed on its own
(it renders inside the table's <caption> element), while variable_level
lets downstream reporting engines promote the same text to a dedicated
subtitle row.
modify_split_caption(
x,
spl_col,
pattern = "Parameter: {spl_level}",
hide_spl_col = TRUE
)(tbl_split, list, or gtsummary)
a table split with gtsummary::tbl_split_by_rows(), the list of split
tables, or a single split page.
(string)
name of the column the table was split by (the variable_level passed to
gtsummary::tbl_split_by_rows()). Used to hide that column on each page
when hide_spl_col = TRUE.
(string)
a glue() pattern used to build each page's subtitle from the split level.
The {spl_level} token is replaced by the split value. Defaults to
"Parameter: {spl_level}", matching parameter-split tables such as
laboratory tables split by PARAM.
(flag)
whether to hide spl_col on each split page, since it is redundant after a
single-level split. Defaults to TRUE. Silently does nothing when the
column is absent or already hidden.
an object of the same class as x, with the split subtitle set as a
caption and on the variable_level attribute (and the split column hidden)
on each page.
lst <- gtsummary::trial |>
dplyr::select(trt, age, grade) |>
dplyr::arrange(trt) |>
tbl_listing(split_by_rows = list(variable_level = "trt"))
# Split a listing by treatment, then subtitle each page and drop the column
modify_split_caption(lst, spl_col = "trt", pattern = "Treatment: {spl_level}")[[1]]
Treatment: Drug A
Age
Grade
# Keep the split column and use the default "Parameter:" subtitle
modify_split_caption(lst, spl_col = "trt", hide_spl_col = FALSE)[[1]]
Parameter: Drug A
Chemotherapy Treatment
Age
Grade