Some data are inherently grouped, and should be reported together. Grouped variables are all indented together. This function indents the variables that should be reported together while adding a header above the group.
add_variable_group_header(x, header, variables, indent = 4L)(tbl_summary)
gtsummary object of class 'tbl_summary'
(string)
string of the header to place above the variable group
(tidy-select)
Variables to group that appear in x$table_body.
Selected variables should be appear consecutively in table.
(integer)
An integer indicating how many space to indent text.
All rows in the group will be indented by this amount. Default is 4.
a gtsummary table
This function works by inserting a row into the x$table_body and
indenting the group of selected variables.
This function cannot be used in conjunction with all functions in gtsummary;
for example, bold_labels() will bold the incorrect rows after running
this function.
# Example 1 ----------------------------------
set.seed(11234)
data.frame(
exclusion_age = sample(c(TRUE, FALSE), 20, replace = TRUE),
exclusion_mets = sample(c(TRUE, FALSE), 20, replace = TRUE),
exclusion_physician = sample(c(TRUE, FALSE), 20, replace = TRUE)
) |>
tbl_summary(
label = list(exclusion_age = "Age",
exclusion_mets = "Metastatic Disease",
exclusion_physician = "Physician")
) |>
add_variable_group_header(
header = "Exclusion Reason",
variables = starts_with("exclusion_")
) |>
modify_caption("**Study Exclusion Criteria**")
Study Exclusion Criteria
Characteristic
N = 201
1 n (%)
# Example 2 ----------------------------------
lm(marker ~ trt + grade + age, data = trial) |>
tbl_regression() |>
add_global_p(keep = TRUE, include = grade) |>
add_variable_group_header(
header = "Treatment:",
variables = trt
) |>
add_variable_group_header(
header = "Covariate:",
variables = -trt
) |>
# indent levels 8 spaces
modify_indent(
columns = "label",
rows = row_type == "level",
indent = 8L
)
Characteristic
Beta
95% CI
p-value
Abbreviation: CI = Confidence Interval