This function stratifies your data frame, builds gtsummary tables, and
stacks the resulting tables in a nested style. The underlying functionality
is similar to tbl_strata(), except the resulting tables are nested or indented
within each group.
NOTE: The header from the first table is used for the final table. Oftentimes, this header will include incorrect Ns and must be updated.
Usage
tbl_strata_nested_stack(
  data,
  strata,
  .tbl_fun,
  ...,
  row_header = "{strata}",
  quiet = FALSE
)Arguments
- data
 (
data.frame)
a data frame- strata
 (
tidy-select)
character vector or tidy-selector of columns in data to stratify results by. Only observed combinations are shown in results.- .tbl_fun
 (
function) A function or formula. If a function, it is used as is. If a formula, e.g.~ .x %>% tbl_summary() %>% add_p(), it is converted to a function. The stratified data frame is passed to this function.- ...
 Additional arguments passed on to the
.tbl_funfunction.- row_header
 (
string)
string indicating the row headers that appear in the table. The argument usesglue::glue()syntax to insert values into the row headers. Elements available to insert arestrata,n,Nandp. Thestrataelement is the variable level of the strata variables. Default is'{strata}'.- quiet
 (scalar
logical)
Logical indicating whether to suppress additional messaging. Default isFALSE.
Examples
# Example 1 ----------------------------------
tbl_strata_nested_stack(
  trial,
  strata = trt,
  .tbl_fun = ~ .x |>
    tbl_summary(include = c(age, grade), missing = "no") |>
    modify_header(all_stat_cols() ~ "**Summary Statistics**")
)
  Characteristic 
      Summary Statistics1 
    
 
 
 
 
 
 
 
 
 
 
 
 1 Median (Q1, Q3); n (%) 
    
# Example 2 ----------------------------------
tbl_strata_nested_stack(
  trial,
  strata = trt,
  .tbl_fun = ~ .x |>
    tbl_summary(include = c(age, grade), missing = "no") |>
    modify_header(all_stat_cols() ~ "**Summary Statistics**"),
  row_header = "{strata}, n={n}"
) |>
  # bold the row headers; print `x$table_body` to see hidden columns
  modify_bold(columns = "label", rows = tbl_indent_id1 > 0)
  Characteristic 
      Summary Statistics1 
    
 
 
 
 
 
 
 
 
 
 
 
 1 Median (Q1, Q3); n (%) 
    
