Selectors are the mechanism flextable uses to target specific cells when applying formatting, styling, or content operations. Nearly every formatting function —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.
bold(), color(), bg(), padding(), align(), hline(), and more — accepts three selector arguments: i, j, and part.
When all three default to NULL (rows) or their default string, the operation applies to the entire part.
The part argument
The part argument determines which section of the table is targeted.
When
part = "all", the operation is applied to each part independently. Formula row selectors (i = ~ condition) cannot be used with part = "all", "header", or "footer" — these parts store only character representations of values, so numeric conditions cannot be evaluated on them.The i argument — row selector
The i argument selects which rows within a part to target. When i = NULL (the default), all rows in the part are selected.
- Formula (body only)
- Integer index
- Logical vector
Use a one-sided formula to filter rows by a condition evaluated against the body dataset. The expression uses column names from the original Formula selectors evaluate conditions using the actual data types from the dataset (numeric, logical, etc.). They only work with
data.frame.part = "body".Targeting the last row with nrow_part()
nrow_part() returns the number of rows in a given part. It is the recommended way to programmatically select the last row:
The j argument — column selector
The j argument selects which columns to target. When j = NULL (the default), all columns are selected.
- Character names
- Formula
- Integer index
- Logical vector
The recommended approach — explicit column names are readable and resilient to column reordering:
Combining i, j, and part
Selectors can be combined freely. The operation applies to the intersection of selected rows and selected columns in the targeted part:
Selectors and merged cells
Selectors work with merged cells — they target the individual cells by row and column index, not by the visual span. When you format a merged cell, apply formatting to the top-left cell of the merged region (the anchor cell); other cells in the span are hidden and their formatting has no visual effect.Best practices
Rows
- Use
i = ~ conditionfor conditional body formatting. - Use
nrow_part(ft, "body")to target the last row without hard-coding a number. - Use integers or logical vectors to target header and footer rows.
Columns
- Prefer character names (
j = c("col1", "col2")) for maintainability. - Use formula syntax (
j = ~ col1 + col2) when excluding columns is cleaner. - Avoid integer positions when column order may change.
Quick reference
| Selector | Type | Example | Notes |
|---|---|---|---|
i | Formula | i = ~ mpg < 22 | Body only; uses original data types |
i | Integer | i = c(1, 3) | Any part; positional |
i | Logical | i = c(TRUE, FALSE, ...) | Length must match part row count |
i | NULL | i = NULL | All rows (default) |
j | Character | j = c("mpg", "cyl") | Recommended; column names |
j | Formula | j = ~ mpg + cyl | Column list or exclusion |
j | Integer | j = 1:3 | Positional; negative excludes |
j | Logical | j = c(TRUE, FALSE, ...) | Length must match ncol_keys(ft) |
j | NULL | j = NULL | All columns (default) |
part | String | part = "body" | "body", "header", "footer", "all" |