Skip to contents

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_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

(scalar logical)
Logical indicating whether to suppress additional messaging. Default is FALSE.

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**")
)
Characteristic Summary Statistics1
Drug A
    Age 46 (37, 60)
    Grade
        I 35 (36%)
        II 32 (33%)
        III 31 (32%)
Drug B
    Age 48 (39, 56)
    Grade
        I 33 (32%)
        II 36 (35%)
        III 33 (32%)
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
Drug A, n=98
    Age 46 (37, 60)
    Grade
        I 35 (36%)
        II 32 (33%)
        III 31 (32%)
Drug B, n=102
    Age 48 (39, 56)
    Grade
        I 33 (32%)
        II 36 (35%)
        III 33 (32%)
1 Median (Q1, Q3); n (%)