Skip to contents

Appends two or more ltsummary tables on top of each other. The columns of the result are the union of the input columns, matched by name, and every styling instruction of every input is kept: a hidden tbl_id1 column records which table each row came from, and the stored row selectors are scoped to it. Stacking tables that are themselves stacked adds tbl_id2 outside tbl_id1, and so on. The interface is that of gtsummary::tbl_stack().

With group_header, each table is labelled with a full-width group row. The group column renders as lt separator rows, and as.data.frame() shows the label on the first row of each group. bold_labels() and friends keep targeting the label column of the stacked table, not the group rows.

Header labels are taken from the first table that has them; use attr_order to give a later table precedence. The attributes a table can hold only one of, its caption and its horizontal rule, always come from the first table in tbls that has them.

Usage

tbl_stack(
  tbls,
  group_header = NULL,
  quiet = FALSE,
  attr_order = seq_along(tbls),
  tbl_ids = NULL,
  tbl_id_lbls = NULL
)

Arguments

tbls

(list)
List of ltsummary tables.

group_header

(character)
Header placed above each table's rows, one per table. Default is NULL, for no group rows.

quiet

(scalar logical)
Whether to suppress the message listing the column headers when they differ between the tables. Default is FALSE.

attr_order

(integer)
The order in which the tables' header labels take precedence. attr_order = 2 uses the headers of the second table. Default is the order of tbls.

tbl_ids

(character)
Optional names for the input tables, one per table, used to name the tbls element of the result.

tbl_id_lbls

(vector)
Optional labels, one per table, stored in a hidden tbl_id1_lbl column of the result's table_body. Mostly for the development of other functions.

Value

A table of class c("tbl_stack", "ltsummary") whose tbls element holds the input tables.

Examples

# Example 1 ----------------------------------
# an unadjusted and an adjusted model of the same term
t1 <- glm(response ~ trt, trial, family = binomial) |>
  tbl_regression(exponentiate = TRUE, label = list(trt = "Treatment (unadjusted)"))
t2 <- glm(response ~ trt + grade + stage + marker, trial, family = binomial) |>
  tbl_regression(include = trt, exponentiate = TRUE, label = list(trt = "Treatment (adjusted)"))
tbl_stack(list(t1, t2))
# Example 2 ---------------------------------- # group headers label the two models t3 <- glm(death ~ trt, trial, family = binomial) |> tbl_regression(exponentiate = TRUE) tbl_stack(list(t1, t3), group_header = c("Tumor Response", "Death"))