Cell merging combines adjacent cells so their content is displayed in a single larger cell. flextable tracks merge state internally using span matrices, so text and formatting applied before or after merging are both preserved in the visible (top-left) cell.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.
When to merge
- Grouped rows: merge the category column vertically when consecutive rows share the same value.
- Spanning headers: merge header cells that describe a group of columns.
- Summary rows: merge across all columns in a total or note row.
Merge horizontally — consecutive identical cells
merge_h() scans each selected row and merges runs of consecutive cells that display identical text:
i selector to restrict merging to specific rows:
part argument defaults to "body". Pass part = "header" to merge header rows:
Merge vertically — consecutive identical cells
merge_v() scans each selected column and merges runs of consecutive cells that display identical text:
Merging based on a key column
Thetarget argument lets you drive merges in multiple columns using the grouping of a single key column:
j = "srdr_id" determines where row runs start and end, but the merge is also applied to the "substances" column.
Combined column matching
Setcombine = TRUE to base the grouping on the concatenated values of all columns in j rather than inspecting each column independently:
Merge a specific rectangle of cells
merge_at() merges an arbitrary contiguous block defined by row and column selectors. All rows and columns must be consecutive:
Merge a column range within specific rows
merge_h_range() merges a contiguous range of columns — from j1 to j2 — for every selected row. Unlike merge_h(), it does not require cell values to match:
Remove all merges
merge_none() resets all span information back to no merging. By default it applies to all parts:
part to target only one section:
Merging and formatting
Merging affects which cell holds the visible content, but it does not change the underlying data or the styles already applied. After merging:- The content and style of the top-left cell in the merged region is displayed.
- Styles applied after merging via
bg(),color(), etc. target cells by their original row/column positions — the merge simply hides the non-primary cells. - Use
fix_border_issues()after merging if borders look inconsistent; merging can expose shared borders that need reconciliation.
Multi-level headers with add_header_row()
add_header_row() creates spanning cells directly when you pass colwidths, so you do not need to call any merge function for those rows:
merge_h() and merge_v() on header rows only when you built the header with set_header_df() or add_header() and identical labels need to be collapsed.