Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/davidgohel/flextable/llms.txt

Use this file to discover all available pages before exploring further.

These functions add or remove header and footer rows in a flextable, and provide tools for splitting compound column names into multi-row headers.

add_header_row()

Add a single row to the header where labels can span multiple columns.
add_header_row(x, top = TRUE, values = character(0), colwidths = integer(0))
x
flextable
required
A flextable object.
top
logical
default:"TRUE"
Insert the row at the top (TRUE) or bottom (FALSE) of the existing header.
values
character | list | paragraph
Labels to add. A character vector, a list, or a call to as_paragraph() for formatted content. The number of labels must match the number of span groups defined by colwidths.
colwidths
integer
Number of columns each label spans. Must sum to the total number of columns. Defaults to one column per label.

Example

ft <- flextable(head(airquality))
ft <- add_header_row(
  ft,
  values = c("Measure", "Time"),
  colwidths = c(4, 2),
  top = TRUE
)
ft <- theme_box(ft)
ft

add_header_lines()

Add one or more full-width rows to the header. Each label spans all columns.
add_header_lines(x, values = character(0), top = TRUE)
x
flextable
required
A flextable object.
values
character | paragraph
A character vector or a call to as_paragraph(). Each element becomes a separate full-width row.
top
logical
default:"TRUE"
Insert rows at the top (TRUE) or bottom (FALSE) of the existing header.

Example

ft <- flextable(head(iris))
ft <- add_header_lines(ft, values = c("Table title", "Subtitle here"))
ft <- autofit(ft)
ft

add_header()

Add a header row where each value maps to a named column. Unlike add_header_row(), values fill exactly one column.
add_header(x, top = TRUE, ..., values = NULL)
x
flextable
required
A flextable object.
top
logical
default:"TRUE"
Insert the row at the top or bottom of the existing header.
...
name-value pairs
Named arguments where names are column keys and values are the labels to insert.
values
list
A named list of column key-value pairs. If supplied, ... is ignored.

Example

new_row <- list(
  Sepal.Length = c("min: 4.3", "max: 7.9"),
  Sepal.Width  = c("min: 2.0", "max: 4.4")
)
ft <- flextable(head(iris))
ft <- add_header(ft, values = new_row, top = FALSE)
ft <- theme_booktabs(ft, bold_header = TRUE)
ft

Add a single row to the footer where labels can span multiple columns.
add_footer_row(x, top = TRUE, values = character(0), colwidths = integer(0))
x
flextable
required
A flextable object.
top
logical
default:"TRUE"
Insert the row at the top or bottom of the existing footer.
values
character | list | paragraph
Labels to add. A character vector, a list, or a call to as_paragraph().
colwidths
integer
Number of columns each label spans. Must sum to the total number of columns.

Example

ft <- flextable(head(airquality))
ft <- add_footer_row(
  ft,
  values = c("Source", "Year"),
  colwidths = c(4, 2),
  top = TRUE
)
ft

Add one or more full-width rows to the footer. Each label spans all columns.
add_footer_lines(x, values = character(0), top = FALSE)
x
flextable
required
A flextable object.
values
character | paragraph
A character vector or a call to as_paragraph(). Each element becomes a separate full-width row.
top
logical
default:"FALSE"
Insert rows at the top (TRUE) or bottom (FALSE) of the existing footer.

Example

ft <- flextable(head(iris))
ft <- add_footer_lines(ft, values = c("Source: Fisher, 1936", "n = 6"))
ft

Add a footer row where each value maps to a named column.
add_footer(x, top = TRUE, ..., values = NULL)
x
flextable
required
A flextable object.
top
logical
default:"TRUE"
Insert the row at the top or bottom of the existing footer.
...
name-value pairs
Named arguments where names are column keys and values are the labels to insert.
values
list
A named list of column key-value pairs. If supplied, ... is ignored.

Example

new_row <- as.list(colMeans(iris[, -5]))
new_row$Species <- "Means"

ft <- flextable(head(iris))
ft <- add_footer(ft, values = new_row)
ft

delete_part()

Remove an entire part (header, footer, or body) from a flextable.
delete_part(x, part = "header")
x
flextable
required
A flextable object.
part
string
default:"header"
The part to remove: "header", "footer", or "body".

Example

ft <- flextable(head(iris))
ft <- delete_part(x = ft, part = "header")
ft

separate_header()

Split compound column names (e.g., Sepal.Length or Sepal_Length) into multiple header rows using a separator.
separate_header(
  x,
  opts = c("span-top", "center-hspan", "bottom-vspan", "default-theme"),
  split = "[_\\.]",
  fixed = FALSE
)
x
flextable
required
A flextable object. Must have only one header row when this function is called.
opts
character
Optional post-processing operations as a character vector:
  • "span-top" — span empty cells upward with the first non-empty cell, column by column.
  • "center-hspan" — center cells that are horizontally spanned.
  • "bottom-vspan" — align to the bottom cells affected by "span-top".
  • "default-theme" — apply the theme set in set_flextable_defaults(theme_fun = ...) to the new header.
split
string
default:"[_\\\\\\\\.]"
A regular expression used to split column names. Default splits on underscores and dots.
fixed
logical
default:"FALSE"
If TRUE, treat split as a fixed string rather than a regular expression.

Example

x <- data.frame(
  Species          = as.factor(c("setosa", "versicolor", "virginica")),
  Sepal.Length_mean = c(5.006, 5.936, 6.588),
  Sepal.Length_sd   = c(0.352, 0.516, 0.636),
  Sepal.Width_mean  = c(3.428, 2.770, 2.974),
  Sepal.Width_sd    = c(0.379, 0.314, 0.323)
)

ft <- flextable(x)
ft <- colformat_double(ft, digits = 2)
ft <- theme_box(ft)
ft <- separate_header(x = ft, opts = c("span-top", "bottom-vspan"))
ft

Return value

All functions return the modified flextable object.

See also

Build docs developers (and LLMs) love