Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

bg

Set background color


Description

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

Usage

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

Arguments

x

a flextable object

i

rows selection

j

columns selection

bg

color to use as background 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 bg. This is only useful if j is colored with values contained in another (or other) column.

Illustrations

Note

Word does not allow you to apply transparency to table cells or paragraph shading.

See Also

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

Examples

ft_1 <- flextable(head(mtcars))
ft_1 <- bg(ft_1, bg = "wheat", part = "header")
ft_1 <- bg(ft_1, i = ~ qsec < 18, bg = "#EFEFEF", part = "body")
ft_1 <- bg(ft_1, j = "drat", bg = "#606060", part = "all")
ft_1 <- color(ft_1, j = "drat", color = "white", part = "all")
ft_1

if(require("scales")){
  ft_2 <- flextable(head(iris))
  colourer <- col_numeric(
    palette = c("wheat", "red"),
    domain = c(0, 7))
  ft_2 <- bg(ft_2, j = c("Sepal.Length", "Sepal.Width",
                     "Petal.Length", "Petal.Width"),
           bg = colourer, part = "body")
  ft_2
}

flextable

Functions for Tabular Reporting

v0.6.10
GPL-3
Authors
David Gohel [aut, cre], Clementine Jager [ctb], Quentin Fazilleau [ctb], Maxim Nazarov [ctb] (rmarkdown for docx output), Titouan Robert [ctb], Michael Barrowman [ctb] (inline footnotes), Atsushi Yasumoto [ctb] (support for bookdown cross reference), Paul Julian [ctb] (support for gam objects)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.