This function is for developers. It does little checking of its
arguments, by design. Most users should reach for the functions built on
it: modify_header(), modify_spanning_header(), modify_column_hide(),
modify_column_unhide(), modify_footnote_header(),
modify_footnote_body(), modify_abbreviation(),
modify_column_alignment(), modify_fmt_fun(), modify_indent(),
modify_column_merge(), modify_missing_symbol(), modify_bold() and
modify_italic().
modify_table_styling() records styling instructions for one or more
columns by appending to the data frames of x$table_styling. The
object definition article
describes them.
Usage
modify_table_styling(
x,
columns,
rows = NULL,
label = NULL,
spanning_header = NULL,
hide = NULL,
footnote = NULL,
footnote_abbrev = NULL,
align = NULL,
missing_symbol = NULL,
fmt_fun = NULL,
text_format = NULL,
undo_text_format = NULL,
indent = NULL,
text_interpret = "md",
cols_merge_pattern = NULL
)Arguments
- x
(
ltsummary)
An ltsummary object.- 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.- label
(
character)
Column header label(s), recycled overcolumns.- spanning_header
(
string)
Text of a spanning header.- hide
(
scalar logical)
Whether to hide the columns.- footnote
(
string)
Footnote text.- footnote_abbrev
(
string)
An abbreviation definition, e.g."CI = Confidence Interval".- align
(
string)
Column alignment, one ofc("left", "right", "center").- missing_symbol
(
string)
Text shown for missing values.- fmt_fun
(
function)
Function that formats the values in the selected columns and rows.- text_format, undo_text_format
(
string,scalar logical)text_formatis"bold"or"italic";undo_text_format = TRUEremoves the formatting instead of adding it.- indent
(
integer)
Number of spaces of indentation.- text_interpret
(
string)
Howlabel,spanning_header,footnoteandfootnote_abbrevare interpreted:"md"(the default; the markdown subset**bold**,_italic_and line breaks),"html"(raw HTML) or"none"(verbatim).- cols_merge_pattern
(
string)
Glue-style pattern merging columns ofx$table_body, e.g."{conf.low}, {conf.high}". The first column named in the pattern must be the single column passed incolumns; the other columns are hidden.
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().
cols_merge_pattern argument
Merging replaces the first column of the pattern with the merged text after the formatting functions of every column have been applied, so a numeric column becomes character once merged. The merge is recorded and carried out at print time.
See also
Other building blocks:
as_ltsummary(),
modify_table_body()
Examples
# Example 1 ----------------------------------
# bold the label rows of the statistic columns
trial |>
tbl_summary(include = c(age, grade), by = trt) |>
modify_table_styling(
columns = all_stat_cols(),
rows = row_type == "label",
text_format = "bold"
)
# Example 2 ----------------------------------
# a header, an abbreviation and a footnote in one call
trial |>
tbl_summary(include = age, by = trt) |>
modify_table_styling(
columns = all_stat_cols(),
label = c("**A**", "**B**"),
footnote = "Median (IQR)",
footnote_abbrev = "IQR = Interquartile Range"
)