Background color
bg() sets the cell background color:
Word does not support transparent backgrounds on table cells or paragraph shading. Use an explicit color instead of
"transparent" when targeting Word output.Gradient coloring with a function
Whenbg is a function it is called once per source column and must return a character vector of colors:
j is colored by values from a different (possibly hidden) column, pass that column name to the source argument.
Vertical alignment
valign() controls where text sits inside the cell vertically:
"top", "center" (default), "bottom".
Text rotation
rotate() changes the text direction. This is useful for headers with long labels:
| Value | Description | Effective angle |
|---|---|---|
"lrtb" | Left-to-right, top-to-bottom (default) | 0° |
"tbrl" | Top-to-bottom, right-to-left | 90° clockwise |
"btlr" | Bottom-to-top, left-to-right | 270° clockwise |
flextable only rotates by right angles. Arbitrary angles are not supported, ensuring consistent rendering across HTML, PDF, Word, and PowerPoint.
autofit() is used, rotation is ignored. Use dim_pretty() and width() instead of autofit() when working with rotated headers.
Padding
padding() sets whitespace inside the cell, in points. Use the shortcut padding to set all four sides at once, or set sides individually:
In PDF output, only
padding.left and padding.right are respected. padding.top and padding.bottom are ignored due to LaTeX limitations. Use set_table_properties(opts_pdf = list(tabcolsep = 1)) for global horizontal spacing in PDF.Line spacing
line_spacing() sets the space between lines of text inside a cell. 1 is single spacing, 2 is double:
Using fp_cell() for full cell control
The style() function accepts an officer::fp_cell() object that bundles all cell properties. Use it when you need to set several cell properties in one call:
Combining multiple properties with style()
style() sets text (pr_t), paragraph (pr_p), and cell (pr_c) properties in a single call, targeting the same i/j/part selection:
style() when you need to set several property types at once on the same selection. Use the individual convenience functions (bold(), bg(), padding(), etc.) when you only need to change one aspect.
Blank column separator styling
empty_blanks() makes blank columns (columns added via col_keys that are not in the source data) transparent — no borders, transparent background, empty content, and a narrow default width:
Hide cell content
void() replaces the visible text in selected columns with an empty string. The column remains in the layout and keeps its header, but its body values are no longer displayed. This is useful when you have already encoded the values in a neighbouring column via compose():
void() does not modify the underlying data. To remove a column entirely from the table, use the col_keys argument of flextable().Tab stop settings
tab_settings() defines tabulation mark positions in table cell paragraphs. This is especially useful in clinical tables where decimals need to align:
Keeping rows together across pages
keep_with_next() sets the Word “Keep with next” attribute on body rows so they do not break across pages. Apply it to all rows in a group except the last:
paginate() — see Export & Output: Word & PowerPoint.
Quick reference
| Function | Key argument | Default part |
|---|---|---|
bg(x, i, j, bg, part) | bg — color string or function | "body" |
valign(x, i, j, valign, part) | valign = "center" | "body" |
rotate(x, i, j, rotation, align, part) | rotation — "lrtb", "tbrl", or "btlr" | "body" |
padding(x, i, j, padding, ...) | padding — shortcut for all four sides | "body" |
line_spacing(x, i, j, space, part) | space = 1 | "body" |
style(x, i, j, pr_t, pr_p, pr_c, part) | pr_t, pr_p, pr_c | "body" |
empty_blanks(x, width, unit, part) | clears blank separator columns | "all" |
void(x, j, part) | hides cell content without removing column | "body" |
keep_with_next(x, i, value, part) | Word “keep with next” on rows | "body" |
tab_settings(x, i, j, value, part) | sets tab stop positions | "body" |