flextable (version 0.9.5)

color: Set font color

Description

Change text color of selected rows and columns of a flextable. A function can be used instead of fixed colors.

When color is a function, it is possible to color cells based on values located in other columns, using hidden columns (those not used by argument colkeys) is a common use case. The argument source has to be used to define what are the columns to be used for the color definition and the argument j has to be used to define where to apply the colors and only accept values from colkeys.

Usage

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

Arguments

x

a flextable object

i

rows selection

j

columns selection

color

color to use as font color. If a function, function need to return a character vector of colors.

part

partname of the table (one of 'all', 'body', 'header', 'footer')

source

if color is a function, source is specifying the dataset column to be used as argument to color. This is only useful if j is colored with values contained in other columns.

See Also

Other sugar functions for table style: align(), bg(), bold(), empty_blanks(), font(), fontsize(), highlight(), italic(), keep_with_next(), line_spacing(), padding(), rotate(), tab_settings(), valign()

Examples

Run this code
ft <- flextable(head(mtcars))
ft <- color(ft, color = "orange", part = "header")
ft <- color(ft,
  color = "red",
  i = ~ qsec < 18 & vs < 1
)
ft

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
  )

  ft_2 <- flextable(x)
  ft_2 <- color(ft_2, j = x$colname, color = scale)
  ft_2 <- set_formatter_type(ft_2)
  ft_2
}

Run the code above in your browser using DataLab