Build {gtsummary} with ARDs

We will use the tbl_summary() function to illustrate how to build a new gtsummary table.

Imagine we wish to construct the table below: it can be done both by passing a data frame to tbl_summary() or an ARD to tbl_ard_summary(). These functions share most of their internals.

library(gtsummary)
library(cards)

tbl_summary(
  trial, 
  by = trt, 
  include = c(marker, age, grade),
  type = age ~ "continuous2",
  statistic = all_continuous2() ~ c("{mean} ({sd})", "{median} ({p25}, {p75})"),
  missing = "no"
)

Characteristic

Drug A
N = 98

1

Drug B
N = 102

1
Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21)
Age

    Mean (SD) 47 (15) 47 (14)
    Median (Q1, Q3) 46 (37, 60) 48 (39, 56)
Grade

    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
1

Median (Q1, Q3); n (%)

# build the ARD with the {cards} package
ard_for_table <- 
  ard_stack(
    trial,
    ard_continuous(variables = c(marker, age)),
    ard_categorical(variables = grade),
    .by = trt,
    .missing = TRUE,                     # we use missing information in gtsummary
    .attributes = TRUE,                  # we also use attributes like column labels
    .total_n = TRUE                      # add total N to the ARD
  )

# then convert it to a table with `tbl_ard_summary()`
ard_for_table |> 
  tbl_ard_summary(
    by = trt,
    type = age ~ "continuous2",
    statistic = all_continuous2() ~ c("{mean} ({sd})", "{median} ({p25}, {p75})")
  )

Characteristic

Drug A

1

Drug B

1
Marker Level (ng/mL) 0.8 (0.2, 1.6) 0.5 (0.2, 1.2)
Age

    Mean (SD) 47.0 (14.7) 47.4 (14.0)
    Median (Q1, Q3) 46.0 (37.0, 60.0) 48.0 (39.0, 56.0)
Grade

    I 35 (35.7%) 33 (32.4%)
    II 32 (32.7%) 36 (35.3%)
    III 31 (31.6%) 33 (32.4%)
1

Median (Q1, Q3); n (%)