Selectors are a core feature of flextable that allow you to specify which
parts (part), rows (i) and columns (j) should be affected by formatting,
styling, or content operations.
Many flextable functions support these selectors, including bg(),
bold(), color(), padding(), fontsize(), italic(), align(),
mk_par(), hline(), vline(), and many others.
Selectors make conditional formatting easy and enable seamless piping of
multiple operations using %>% or |>.
Row selection with the i parameter
The i parameter is used to select specific rows in a flextable for
formatting, styling, or content operations.
When i = NULL (the default), operations apply to all rows.
i = ~ condition # Formula (body only)
i = 1:5 # Integer vector
i = c(TRUE, FALSE, ...) # Logical vector
i = NULL # All rows (default)Best practices:
Use formulas (
i = ~ condition) for conditional selection in bodyUse integers for positional selection in any part.
Use
nrow_part()for dynamic row selection
Column selection with the j parameter
The j parameter is used to select specific columns in a flextable for
formatting, styling, or content operations.
When j = NULL (the default), operations apply to all columns.
j = ~ col1 + col2 # Formula (select multiple)
j = ~ . - col1 # Formula (exclude columns)
j = c("col1", "col2") # Character vector (recommended)
j = 1:5 # Integer vector
j = c(TRUE, FALSE, ...) # Logical vector
j = NULL # All columns (default)Best practices:
Use character vectors (
j = c("col1", "col2")) for clarity and maintainability.Use formulas (
j = ~ col1 + col2) for excluding columns.Avoid integer positions when possible (less maintainable).
Column selectors work with all parts (header, body, footer, all).
Part selection with the part parameter
The part parameter specifies which section of the flextable should be
affected by formatting, styling, or content operations.
part = "body" # Data rows (default)
part = "header" # Header rows
part = "footer" # Footer rows
part = "all" # All partsWhen
part = "body"(the default), operations apply only to the data rows.When
part = "all", the operation is applied to each part independently.
Formula row selectors (i = ~ condition) CANNOT be used with:
part = "header"- Headers contain only character valuespart = "footer"- Footers contain only character valuespart = "all"- Header/footer are character-only while body has original data types
This restriction exists because formula selectors evaluate conditions using
the actual data types from your dataset (numeric, logical, etc.). Headers and
footers store only character representations of values, so conditional
expressions like i = ~ price < 330 cannot be evaluated on them.
Advanced Programming with Selectors
Function nrow_part() returns the number of lines in a part.
# Format the last row differently
ft <- flextable(iris[48:52,])
ft <- bold(ft, i = nrow_part(ft, part = "body"))
# Add footnote to last row
ft <- footnote(
ft,
i = nrow_part(ft, part = "body"), j = 1:4,
value = as_paragraph("Calculated mean")
)
ftFunction ncol_keys() returns the number of columns.
