header, body, and footer. Each part holds one or more rows. The functions on this page add, rename, or remove rows in the header and footer.
Rename column labels
set_header_labels() changes the display text in the bottom header row. It does not add a new row — it replaces existing labels.
values argument, or an unnamed character vector the same length as the number of columns:
Add a spanning header row
add_header_row() inserts a single new row into the header. The colwidths argument controls how many columns each label spans. All colwidths values must sum to the total number of columns in the table.
add_header_row() multiple times to build a multi-level header. Each call adds exactly one row:
Formatted spanning labels
Pass a call toas_paragraph() as the values argument to use rich formatted text in spanning header labels:
Add full-width header lines
add_header_lines() adds one or more rows where the label spans all columns. This is useful for table titles or sub-titles placed above the column headers.
top = FALSE to append lines below the existing header rows instead of prepending above them.
Add header rows one column at a time
add_header() inserts a new row where you supply one value per column using named arguments. Columns you do not name display as empty.
When
add_header() inserts repeated values across rows, combine it with merge_h() and merge_v() to collapse identical adjacent cells.Set the entire header from a data frame
set_header_df() replaces the entire header structure using a mapping data frame. This is convenient for complex multi-row headers defined outside the table:
key column is used to join the mapping to the flextable columns. All other columns in the data frame become header rows, ordered left to right as top to bottom.
Split compound column names automatically
separate_header() splits column names that contain a separator (by default _ or .) into multiple header rows automatically:
opts argument accepts one or more of:
| Option | Effect |
|---|---|
"span-top" | Merges empty upper cells with the first non-empty cell above, column by column |
"center-hspan" | Centers text in horizontally spanned cells |
"bottom-vspan" | Bottom-aligns cells that were vertically spanned by "span-top" |
"default-theme" | Applies the theme set in set_flextable_defaults(theme_fun = ...) to the new header |
Footer rows
Alladd_header_* functions have direct footer equivalents. The default for top in footer functions is FALSE (append below).
Spanning footer row
Full-width footer lines
add_footer_lines() is the most common way to add footnotes and source notes:
Footer rows one column at a time
Set the entire footer from a data frame
set_footer_df() works like set_header_df() but targets the footer part:
Remove a part entirely
delete_part() removes all rows from header, footer, or body:
add_header_row() after clearing the default one.
Add rows to the body
add_body_row() inserts a single row into the body where labels can span multiple columns. This is useful for adding summary rows or dividers inside the table body.
add_body() inserts a row using named column values, similar to add_header():
When
values is a character vector, the column data types in the body are coerced to character. Use a named list to preserve column types, which allows conditional formatting to continue working correctly.Delete rows and columns
delete_rows() removes one or more rows from a table part:
delete_columns() removes one or more columns entirely. Any previously defined spans involving those columns are cleared: