Merge headers in flextables created from gtsummary objects
Source:R/merge-headers.R
merge_headers.RdVertically extends visible header cells through contiguous blank padding cells immediately above them when the resulting merged area is rectangular. Ordinary column labels and spanning headers are treated equally.
Arguments
- x
A
flextableobject, typically produced bygtsummary::as_flex_table().
Details
This function is intended primarily for flextable objects produced by
gtsummary::as_flex_table(), which uses " " (a single space) for missing
spanning-header cells. A cell containing exactly " " is therefore treated
as structural padding. Call this function immediately after conversion and
before applying additional header styling.
Existing horizontal blank spans are split only where required for a vertical merge. Header content is never crossed, and cells that are already merged vertically are left unchanged.
Examples
gtsummary::tbl_summary(
gtsummary::trial,
by = "trt",
include = c("age", "marker")
) |>
gtsummary::modify_spanning_header(
gtsummary::all_stat_cols() ~ "**Treatment Received**"
) |>
gtsummary::as_flex_table() |>
merge_headers()
Characteristic
Treatment Received
Drug A
N = 981
Drug B
N = 1021
Age
46 (37, 60)
48 (39, 56)
Unknown
7
4
Marker Level (ng/mL)
0.84 (0.23, 1.60)
0.52 (0.18, 1.21)
Unknown
6
4
1Median (Q1, Q3)