Skip to contents

Update the functions that format the numeric columns of x$table_body, such as p.value after add_p() or a column added with modify_table_body(). The statistic columns of a tbl_summary table (stat_0, stat_1, ...) are already character, so their formatting is set in tbl_summary(digits =) instead.

Usage

modify_fmt_fun(x, ..., rows = NULL)

Arguments

x

(ltsummary)
An ltsummary object.

...

(formula-list)
Formatting functions by column. modify_fmt_fun(colname = <function>) updates a single column, and a formula selects columns, e.g. modify_fmt_fun(c(estimate, conf.low, conf.high) ~ label_style_sigfig()). Run show_header_names() to see the column names.

rows

(predicate expression)
Expression selecting rows of x$table_body, e.g. row_type == "label". Used for footnotes, formatting functions, missing symbols, indentation and text formatting. Default is NULL, which selects every row (or, for a footnote, places it in the column header). See the section below.

Value

An ltsummary object.

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().

Examples

# Example 1 ----------------------------------
# show p-values to 3 decimal places
trial |>
  tbl_summary(by = trt, include = c(age, grade), missing = "no") |>
  add_p() |>
  modify_fmt_fun(p.value = label_style_pvalue(digits = 3))
# Example 2 ---------------------------------- # different formatting in different rows: the function can be called repeatedly trial |> tbl_summary(by = trt, include = c(age, marker, grade), missing = "no") |> add_p() |> modify_fmt_fun(p.value = label_style_pvalue(digits = 2), rows = variable == "grade") |> modify_fmt_fun(p.value = label_style_pvalue(digits = 3), rows = variable != "grade")