Reputation: 11
I am using gt Package from github and would like to background-colorize all entries in a column of a table which equal the value "0". I found e.g. the following code in the Internet:
# Get a palette of 8 pastel colors from
# the RColorBrewer package
pal <- RColorBrewer::brewer.pal(8, "Pastel2")
# Create lighter and darker variants
# of the base palette (one step lower, one
# step higher)
pal_darker <- pal %>% adjust_luminance(-1.0)
pal_lighter <- pal %>% adjust_luminance(+1.0)
# Create a tibble and make a gt table
# from it; color each column in order of
# increasingly darker palettes (with
# `data_color()`)
tab_1 <-
dplyr::tibble(a = 1:8, b = 1:8, c = 1:8) %>%
gt() %>%
data_color(
columns = vars(a),
colors = scales::col_numeric(
palette = pal_lighter,
domain = c(1, 8)
)
) %>%
data_color(
columns = vars(b),
colors = scales::col_numeric(
palette = pal,
domain = c(1, 8)
)
) %>%
data_color(
columns = vars(c),
colors = scales::col_numeric(
palette = pal_darker,
domain = c(1, 8)
)
)
tab_1
But all of those solutions I found are about coloring a range of cells with a range of colors. Is it also possible with a single color on single cells based on a condition?
Thank you!
Upvotes: 1
Views: 1526
Reputation: 1922
Since you do not provide the value of pal in your code, I have omitted it here. But the example shows coloring at the cell level, with a condition that the value of column c is greater than 3.
library(gt)
tab_1 <-
dplyr::tibble(a = 1:8, b = 1:8, c = 1:8) %>%
gt() %>%
data_color(
columns = vars(a),
colors = scales::col_numeric(
palette = c('red'),
domain = c(1, 8)
)
) %>%
data_color(
columns = vars(b),
colors = scales::col_numeric(
palette = 'green',
domain = c(1, 8)
)
) %>%
tab_style(
style = cell_fill(color = 'grey'),
locations = cells_body(
columns = vars(c),
rows = c > 3
))
tab_1
Upvotes: 1