For advanced manipulation of a table. Applies a function to
x$table_body, the data frame that is printed as the table, and keeps the
styling instructions in sync: styling for columns that disappear is
dropped, and new columns are added hidden. To show a new column, give it a
header with modify_header() or call modify_column_unhide().
See also
Other building blocks:
as_ltsummary(),
modify_table_styling()
Examples
# Example 1 ----------------------------------
# add a column with the difference in median age, formatted and shown
medians <- tapply(trial$age, trial$trt, median, na.rm = TRUE)
trial |>
tbl_summary(by = trt, include = c(age, marker), missing = "no") |>
modify_table_body(function(body) {
body$diff <- ifelse(body$variable == "age", medians[["Drug B"]] - medians[["Drug A"]], NA)
body
}) |>
modify_header(diff = "**Difference in medians**") |>
modify_fmt_fun(diff = label_style_number(digits = 1))
# Example 2 ----------------------------------
# reorder the rows: the missing row first
trial |>
tbl_summary(include = age) |>
modify_table_body(function(body) body[c(2, 1), ])