Learn R Programming

flextable (version 0.9.11)

highlight: Set text highlight color

Description

Change the text highlight 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 must be used to define the columns to be used for the color definition, and the argument j must be used to define where to apply the colors and only accepts values from colkeys.

Usage

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

Arguments

x

a 'flextable' object, see flextable-package to learn how to create 'flextable' object.

i

row selector, see section Row selection with the i parameter in <Selectors in flextable>.

j

column selector, see section Column selection with the j parameter in <Selectors in flextable>.

color

color to use as text highlighting color. If a function, the function must return a character vector of colors.

part

part selector, see section Part selection with the part parameter in <Selectors in flextable>. Value 'all' can be used.

source

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

See Also

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

Examples

Run this code
my_color_fun <- function(x) {
  out <- rep("yellow", length(x))
  out[x < quantile(x, .75)] <- "pink"
  out[x < quantile(x, .50)] <- "wheat"
  out[x < quantile(x, .25)] <- "gray90"
  out
}
ft <- flextable(head(mtcars, n = 10))
ft <- highlight(ft, j = "disp", i = ~ disp > 200, color = "yellow")
ft <- highlight(ft, j = ~ drat + wt + qsec, color = my_color_fun)
ft

Run the code above in your browser using DataLab