Apply a function to the cell text after the formatting functions have run. The function receives the formatted character values of the selected cells and returns replacements, which makes it the place for text substitutions.
Arguments
- x
(
ltsummary)
An ltsummary object.- fmt_fun
(
function)
A function applied to the formatted text of the selected columns and rows.- columns
(selector)
Columns ofx$table_body.- rows
(predicate
expression)
Expression selecting rows ofx$table_body, e.g.row_type == "label". Used for footnotes, formatting functions, missing symbols, indentation and text formatting. Default isNULL, which selects every row (or, for a footnote, places it in the column header). See the section below.
rows argument
The rows argument is a predicate expression that selects the rows the
styling applies to. It is evaluated in x$table_body, so its column names
are available as variables, and it must give a logical vector. To style
the rows of the age variable pass rows = variable == "age"; to style the
variable label rows pass rows = row_type == "label". A logical vector,
a vector of row numbers or a function of table_body are also accepted.
Objects from the calling environment can be used in the expression, e.g.
rows = variable == vars[1], as long as they do not share a name with a
column of x$table_body. The expression is stored, not its result, and
resolved when the table is printed, so it survives later changes to the
table such as remove_row_type().
See also
Other advanced modifiers:
modify_bold_italic,
modify_column_alignment(),
modify_column_hide(),
modify_column_merge(),
modify_fmt_fun(),
modify_indent(),
modify_missing_symbol(),
tbl_butcher()
Examples
# Example 1 ----------------------------------
# show empty categories as "0" instead of "0 (0%)"
data.frame(x = FALSE) |>
tbl_summary(type = x ~ "categorical") |>
modify_post_fmt_fun(
fmt_fun = function(x) ifelse(x == "0 (0%)", "0", x),
columns = all_stat_cols()
)
# Example 2 ----------------------------------
# a different separator between the quartiles
trial |>
tbl_summary(by = trt, include = c(age, marker)) |>
modify_post_fmt_fun(
fmt_fun = function(x) sub(", ", " to ", x, fixed = TRUE),
columns = all_stat_cols(),
rows = var_type == "continuous"
)