tbl_strata_nested_stack: Stratified Nested Stacking

View source: R/tbl_strata_nested_stack.R

tbl_strata_nested_stackR Documentation

Stratified Nested Stacking

Description

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.

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_fun function.

row_header

(string)
string indicating the row headers that appear in the table. The argument uses glue::glue() syntax to insert values into the row headers. Elements available to insert are strata, n, N and p. The strata element is the variable level of the strata variables. Default is '{strata}'.

quiet

[Deprecated]

Value

a stacked 'gtsummary' table

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**")
)

# 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)

gtsummary documentation built on April 3, 2025, 10:18 p.m.