Skip to main content
flextable() creates a flextable object from a data frame. flextable objects support rich text formatting, cell merging, custom headers and footers, and export to Word, PowerPoint, HTML, PDF, and other formats. A flextable is composed of three parts: header, body, and footer. Most formatting functions accept a part argument to target one or all parts.

Function signature

flextable(
  data,
  col_keys = names(data),
  cwidth = 0.75,
  cheight = 0.25,
  defaults = list(),
  theme_fun = theme_booktabs,
  use_labels = TRUE
)

Parameters

data
data.frame
required
A data frame. Also accepts data.table and tibble objects, which are coerced to data.frame internally.
col_keys
character
default:"names(data)"
Column names or keys to display. Names not present in data are added as blank columns.
cwidth
numeric
default:"0.75"
Initial cell width in inches.
cheight
numeric
default:"0.25"
Initial cell height in inches.
defaults
list
deprecated
Deprecated. Use set_flextable_defaults() instead.
theme_fun
function
deprecated
Deprecated. Use set_flextable_defaults() instead.
use_labels
logical
default:"TRUE"
If TRUE, column labels and value labels present in the dataset (e.g., from haven or labelled packages) are used as display headers.

Return value

An object of class flextable.

Special characters

flextable translates \n into a soft return (a line break within the same paragraph, not a new paragraph) and \t into a tab character. Tab rendering varies by output format:
  • HTML — em space entity
  • Word — native Word tab element
  • PowerPoint — native PowerPoint tab element
  • LaTeX\quad

Default formatting

Formatting defaults are applied automatically to every new flextable. Use set_flextable_defaults() to change them globally, init_flextable_defaults() to reset them, and get_flextable_defaults() to inspect the current values.

Examples

ft <- flextable(head(mtcars))
ft
Display only selected columns:
ft <- flextable(
  data = head(iris),
  col_keys = c("Species", "Sepal.Length", "Sepal.Width")
)
ft
Add a blank spacer column:
ft <- flextable(
  data = head(iris),
  col_keys = c("Species", "blank_col", "Sepal.Length")
)
ft

See also