change font color of selected rows and columns of a flextable.

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 bg 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 another (or other) column.

Illustrations

See also

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

Examples

ft <- flextable(head(mtcars))
ft <- color(ft, color = "orange", part = "header")
ft <- color(ft, color = "red",
  i = ~ qsec < 18 & vs < 1 )
ft
#> a flextable object.
#> col_keys: `mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb` 
#> header has 1 row(s) 
#> body has 6 row(s) 
#> original dataset sample: 
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

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
}
#> a flextable object.
#> col_keys: `colname`, `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width` 
#> header has 1 row(s) 
#> body has 4 row(s) 
#> original dataset sample: 
#>                   colname Sepal.Length Sepal.Width Petal.Length Petal.Width
#> Sepal.Length Sepal.Length    1.0000000  -0.1175698    0.8717538   0.8179411
#> Sepal.Width   Sepal.Width   -0.1175698   1.0000000   -0.4284401  -0.3661259
#> Petal.Length Petal.Length    0.8717538  -0.4284401    1.0000000   0.9628654
#> Petal.Width   Petal.Width    0.8179411  -0.3661259    0.9628654   1.0000000