I am using the gt() package in R and am having a rough time trying to style my cells based on their content.
Here's a minimal example.
Let's start with a tibble, my_data
:
my_data <- tibble(person = c('pablo','spots','harry'),
outcome_1 = c('good','bad','good'),
outcome_2 = c('good','good','bad'))
Let's now try to render a gt table and color the cells that contain the text 'bad' so that they're red.
my_data |>
gt() |>
tab_style(
style = list(
cell_fill(color = 'red'),
cell_text(weight = 'bold')),
locations = cells_body(
columns = starts_with('outcome_'),
rows = contains('bad')
)
)
But it doesn't work! The cells containing the word 'bad' should have bold font weight and should have a red background. Instead I get...
The example in the docs for locations
seems fairly straightforward. I am following what it says in for cells_body
usage and as far as I can tell correctly applying the selection helpers starts_with
and contains
.
So what's the problem? I think I got a fundamental misunderstanding here of how this is supposed to work!