
Fill individual cells in a gt table by a condition
Source: R/gt_highlight_cells.R
gt_highlight_cells.RdFills each cell in a block of columns that meets a condition and leaves the
rest alone. It is the per-cell counterpart to gtExtras::gt_highlight_cols()
and gtExtras::gt_highlight_rows(), which fill a whole column or a whole row.
The condition is tested against every cell on its own, so a scattered set of
cells can be filled in one call.
Usage
gt_highlight_cells(
gt_object,
columns,
condition,
fill = "#FFF3B0",
text_color = NULL,
bold = FALSE,
...
)Arguments
- gt_object
A
gttable object to modify.- columns
The block of columns to test, the body of the grid.
- condition
How a cell is chosen. Either a function or a right-hand-side formula applied to each column and returning a logical vector (
~ .x > 0.7,~ grepl("home", .x),~ is.na(.x)), or a logical matrix or data frame the same shape ascolumnsfor a mask computed ahead of time.- fill
Character. The cell fill color. Defaults to
"#FFF3B0", a soft yellow.- text_color
Optional. A hex color for the text in filled cells. Defaults to
NULL, which leaves the text color alone.- bold
Logical. Should filled cells be bolded? Defaults to
FALSE.- ...
Additional arguments passed to
gt::cell_text()for the filled cells, such asstyle = "italic".
Details
A single gt::tab_style() with gt::cells_body() cannot do this, since it
reads rows and columns as a rectangle, every selected row crossed with
every selected column. Filling a diagonal, a checker pattern, or any scatter
otherwise takes a loop over the columns, one pass each. This wraps that loop.
Each selected column is filled independently, so the pattern can be a diagonal, a checker, or any scatter of cells.
The condition reads the underlying data rather than the rendered cell, so a
test like ~ grepl("home", .x) sees whatever value the column holds, HTML and
all. A formula or function must return one logical per row, and NA counts as
not matched. For more than one color, layer the calls, one per color.
See also
gtExtras::gt_highlight_cols() and gtExtras::gt_highlight_rows()
for whole-column and whole-row fills.
Examples
library(gt)
# a correlation matrix: flag strong pairs, but not the diagonal of ones
nba <- subset(sdv_example_standings, league == "nba")
nba$point_diff <- nba$points_for - nba$points_against
vars <- c("wins", "point_diff", "points_for", "points_against", "conference_rank")
m <- round(cor(nba[vars]), 2)
cordf <- data.frame(var = rownames(m), m, row.names = NULL, check.names = FALSE)
cordf |>
gt(rowname_col = "var") |>
gt_highlight_cells(-var, ~ .x > 0.7 & .x < 1, fill = "#FFD1A9") |>
gt_highlight_cells(-var, ~ .x < -0.7, fill = "#A9D0FF")
wins
point_diff
points_for
points_against
conference_rank
wins
point_diff
points_for
points_against
conference_rank
# a grid of team keys, one column per division in finishing order: playoff
# teams one color, the conference's top seed another
afc <- subset(sdv_example_standings, conference == "AFC")
grid <- data.frame(split(afc$team, afc$division), check.names = FALSE)
playoffs <- afc$team[afc$conference_rank <= 7]
top_seed <- afc$team[afc$conference_rank == 1]
gt(grid) |>
gt_highlight_cells(everything(), ~ .x %in% playoffs, fill = "#CCE7F5") |>
gt_highlight_cells(everything(), ~ .x == top_seed, fill = "#FCCF10")
AFC East
AFC North
AFC South
AFC West