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.

flextable provides several functions to merge adjacent cells. Merged cells display only the content of the first cell in the merged range.

Quick reference

FunctionWhat it does
merge_h(x, i, part)Merge identical adjacent cells horizontally
merge_v(x, j, part)Merge identical adjacent cells vertically
merge_at(x, i, j, part)Merge a specific rectangular range
merge_h_range(x, i, j1, j2, part)Merge a column range for selected rows
merge_none(x)Remove all merges
"all" is not a valid value for part in merge functions.

merge_h()

Merges consecutive cells in the same row that have identical formatted values.
merge_h(x, i = NULL, part = "body")
x
flextable
required
A flextable object.
i
integer | formula | logical
Row selector. NULL processes all rows.
part
string
default:"\"body\""
Which part to process: "body", "header", or "footer".

Example

library(flextable)

schedule <- data.frame(
  time = c("9h", "10h", "11h", "14h", "15h", "16h"),
  monday    = c("Math", "Math", "French", "History", "Science", "French"),
  tuesday   = c("English", "Math", "Art", "Math", "Math", "French"),
  wednesday = c("Science", "Math", "Science", "English", "English", "French"),
  stringsAsFactors = FALSE
)

ft <- flextable(schedule)
ft <- theme_box(ft)
ft <- merge_h(ft)
ft

merge_v()

Merges consecutive cells in the same column that have identical formatted values.
merge_v(x, j = NULL, target = NULL, part = "body", combine = FALSE)
x
flextable
required
A flextable object.
j
integer | character
Column selector that controls which columns are inspected for consecutive identical values.
target
character
Column names where the merge should actually be applied. Useful when you want to trigger a merge based on one column’s values but apply it to additional columns. If NULL, defaults to the columns selected by j.
part
string
default:"\"body\""
Which part to process: "body", "header", or "footer".
combine
logical
default:"FALSE"
If TRUE, the columns in j are combined into a single composite key before checking for consecutive identical values. If FALSE, each column is inspected independently.

Example

Merge vertically on two columns:
ft <- flextable(mtcars)
ft <- merge_v(ft, j = c("gear", "carb"))
ft
Merge based on one column but apply the span to additional target columns:
ft <- flextable(data_ex)
ft <- theme_box(ft)
ft <- merge_v(ft,
  j = "srdr_id",
  target = c("srdr_id", "substances")
)
ft

merge_at()

Merges all cells in a specified rectangular range into a single cell. All selected rows and columns must be consecutive.
merge_at(x, i = NULL, j = NULL, part = "body")
x
flextable
required
A flextable object.
i
integer | formula | logical
Row selector.
j
integer | character
Column selector.
part
string
default:"\"body\""
Which part to process: "body", "header", or "footer".

Example

ft <- flextable(head(mtcars), cwidth = .5)
ft <- merge_at(ft, i = 1:2, j = 1:2)
ft

merge_h_range()

Merges a consecutive range of columns into a single cell for each selected row. Both j1 and j2 must define consecutive columns.
merge_h_range(x, i = NULL, j1 = NULL, j2 = NULL, part = "body")
x
flextable
required
A flextable object.
i
integer | formula | logical
Row selector.
j1
integer | character
The first (leftmost) column of the merge range.
j2
integer | character
The last (rightmost) column of the merge range.
part
string
default:"\"body\""
Which part to process: "body", "header", or "footer".

Example

ft <- flextable(head(mtcars), cwidth = .5)
ft <- theme_box(ft)
ft <- merge_h_range(ft, i = ~ cyl == 6, j1 = "am", j2 = "carb")
ft <- align(ft, i = ~ cyl == 6, align = "center")
ft

merge_none()

Removes all cell merging information from a flextable, restoring each cell to independent display.
merge_none(x)
x
flextable
required
A flextable object.

Example

ft <- flextable(head(iris))
ft <- set_header_df(ft, mapping = typology, key = "col_keys")
ft <- merge_v(ft, j = c("Species"))
ft <- theme_tron_legacy(merge_none(ft))
ft

See also

  • valign() — set vertical alignment within merged cells
  • align() — set horizontal alignment

Build docs developers (and LLMs) love