Skip to main content
color() changes the text color of a selection of cells in a flextable. You can supply a fixed color string or a function that derives colors from column values.

Function signature

color(x, i = NULL, j = NULL, color, part = "body", source = j)

Parameters

x
flextable
required
A flextable object.
i
integer | formula | logical
Row selector. Accepts integer indices, a one-sided formula (e.g., ~ col > 5), or a logical vector. NULL selects all rows in the specified part.
j
integer | character | formula
Column selector. Accepts integer indices, column names, a formula, or a logical vector. NULL selects all columns.
color
string | function
required
A CSS-compatible color string (e.g., "red", "#FF0000") or a function that accepts a vector of data values and returns a character vector of color strings.
part
string
default:"body"
Which part of the table to target: "body", "header", "footer", or "all".
source
integer | character | formula
default:"j"
When color is a function, source specifies which dataset column(s) are passed as input to the function. This allows coloring cells in j based on values from a different (possibly hidden) column. Defaults to j.

Return value

The modified flextable object.

Examples

Apply a fixed color to the header:
ft <- flextable(head(mtcars))
ft <- color(ft, color = "orange", part = "header")
ft
Color rows conditionally using a formula:
ft <- flextable(head(mtcars))
ft <- color(ft,
  color = "red",
  i = ~ qsec < 18 & vs < 1
)
ft
Color cells with a scaling function (requires the scales package):
if (require("scales")) {
  scale <- scales::col_numeric(domain = c(-1, 1), palette = "RdBu")
  x <- as.data.frame(cor(iris[-5]))
  x <- cbind(
    data.frame(
      colname = colnames(x),
      stringsAsFactors = FALSE
    ),
    x
  )
}

See also