bg() changes the background color of selected rows and columns. You can supply a fixed color string or a function that maps cell values to colors, which is useful for heat-map-style formatting.
Function signature
Parameters
A
flextable object.Row selector. Accepts integer indices, a one-sided formula (e.g.,
~ qsec < 18), or a logical vector. NULL selects all rows in the specified part.Column selector. Accepts integer indices, column names, a formula, or a logical vector.
NULL selects all columns.Background color to apply. Can be:
- A single color string (e.g.,
"#EFEFEF"or"wheat") applied to all selected cells. - A function that receives a column of data and returns a character vector of colors. Functions from the
scalespackage (e.g.,col_numeric()) work directly.
Which part of the table to target:
"body", "header", "footer", or "all".When
bg is a function, source specifies which dataset column(s) to pass as the argument to that function. This is useful when you want to color cells in column j based on values from a different column (including hidden columns not in colkeys). Defaults to j.Return value
The modifiedflextable object.
Examples
Apply a fixed background color to the header:scales color function for continuous heat-map coloring:
Word does not support transparency in table cell or paragraph shading. Use solid color values only when targeting Word output.
See also
color()— set text colorhighlight()— highlight text with a background colorstyle()— apply multiple formatting properties at once