Extracts the row indices of variable
group headers from a gtsummary table. This is useful
for applying background colors or other gt-specific
styling after converting a gtsummary table to gt
with as_gt().
Arguments
- tbl
A gtsummary table object with variable group headers created by
gtsummary::add_variable_group_header()
Details
Variable group headers are identified by
row_type == 'variable_group' in the table body.
The returned row numbers can be used with
gt::tab_style() to apply styling like background
colors after converting to a gt table.
This function should be called BEFORE converting the table with as_gt(),
as the row type information is only available in gtsummary table objects.
See also
add_group_styling()for applying text formatting to group headersgtsummary::add_variable_group_header()for creating variable groupsgt::tab_style()for applying gt-specific styling
Examples
# \donttest{
# Create table with variable groups
my_tbl <- gtsummary::trial |>
gtsummary::tbl_summary(by = trt, include = c(age, marker, grade, stage)) |>
gtsummary::add_variable_group_header(
header = "Demographics",
variables = age
) |>
gtsummary::add_variable_group_header(
header = "Clinical",
variables = marker:stage
) |>
add_group_styling()
# Get group row numbers before conversion
group_rows <- get_group_rows(my_tbl)
# Convert to gt and apply gray background
my_tbl |>
gtsummary::as_gt() |>
gt::tab_style(
style = gt::cell_fill(color = "#E8E8E8"),
locations = gt::cells_body(rows = group_rows)
)
Characteristic
Drug A
N = 981
Drug B
N = 1021
1 Median (Q1, Q3); n (%)
# }
