Skip to main content
These functions modify the labels displayed in the header or footer of a flextable. set_header_labels() renames individual column labels in the bottom header row. set_header_df() and set_footer_df() rebuild the entire header or footer from a mapping data frame.

set_header_labels()

Change the display labels in the bottom row of the header.
set_header_labels(x, ..., values = NULL)
x
flextable
required
A flextable object.
...
name-value pairs
Named arguments where names are data column names and values are the replacement label strings.
values
named list | character
Alternative to .... A named list (column name to label), or an unnamed character vector with the same length as the number of columns. If supplied, ... is ignored.

Examples

Using named arguments:
ft <- flextable(head(iris))
ft <- set_header_labels(
  ft,
  Sepal.Length = "Sepal length",
  Sepal.Width  = "Sepal width",
  Petal.Length = "Petal length",
  Petal.Width  = "Petal width"
)
ft
Using the values list:
ft <- flextable(head(iris))
ft <- set_header_labels(
  ft,
  values = list(
    Sepal.Length = "Sepal length",
    Sepal.Width  = "Sepal width",
    Petal.Length = "Petal length",
    Petal.Width  = "Petal width"
  )
)
ft
Using an unnamed character vector (positional, all columns must be listed):
ft <- flextable(head(iris))
ft <- set_header_labels(
  x = ft,
  values = c("Sepal length", "Sepal width", "Petal length", "Petal width", "Species")
)
ft

set_header_df()

Replace all header rows using a mapping data frame. Unlike set_header_labels() which modifies only the bottom header row, this function rebuilds the entire header structure. The data frame must contain one column whose values match the flextable col_keys. This column is used as the join key. The remaining columns become header rows — the leftmost column maps to the top header row and the rightmost to the bottom.
set_header_df(x, mapping = NULL, key = "col_keys")
x
flextable
required
A flextable object.
mapping
data.frame
A data frame specifying header content per column. One column must match col_keys; all others become header rows.
key
string
default:"col_keys"
Name of the column in mapping used to join with the flextable column keys.

Example

typology <- data.frame(
  col_keys = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"),
  what     = c("Sepal", "Sepal", "Petal", "Petal", "Species"),
  measure  = c("Length", "Width", "Length", "Width", "Species"),
  stringsAsFactors = FALSE
)

ft <- flextable(head(iris))
ft <- set_header_df(ft, mapping = typology, key = "col_keys")
ft <- merge_h(ft, part = "header")
ft <- merge_v(ft, j = "Species", part = "header")
ft <- theme_vanilla(ft)
ft <- fix_border_issues(ft)
ft

Replace all footer rows using a mapping data frame. Works identically to set_header_df() but targets the footer.
set_footer_df(x, mapping = NULL, key = "col_keys")
x
flextable
required
A flextable object.
mapping
data.frame
A data frame specifying footer content per column.
key
string
default:"col_keys"
Name of the column in mapping used to join with the flextable column keys.

Example

typology <- data.frame(
  col_keys = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"),
  unit     = c("(cm)", "(cm)", "(cm)", "(cm)", ""),
  stringsAsFactors = FALSE
)

ft <- set_footer_df(ft, mapping = typology, key = "col_keys")
ft <- italic(ft, italic = TRUE, part = "footer")
ft <- theme_booktabs(ft)
ft <- fix_border_issues(ft)
ft

Return value

All functions return the modified flextable object.

See also