I am having a rough time trying to style cells in a gt table. I think it's because I am misunderstanding a concept.
Here's an example of what I mean, starting with a tibble...
my_data <- tibble(person = c('pablo','spots','harry'),
outcome_1 = c('good','bad','good'),
outcome_2 = c('good','good','bad'))
What I would to do is to fill the background of all cells in the outcome_1
or outcome_2
column that contain 'bad' with red.
So to do that we just add a tab_style
consisting of 2 arguments:
style
-- a list of styles to apply
locations
-- a thing that somehow identifies where the styles are to be applied
Here's what I tried... but it doesn't work. I am expecting the cells with 'bad' to be colored red and have bold text but instead, they're unstyled.
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')
)
)
Here's what it yields...
I used cells_body
because I want to apply the styles to cells. The examples in the docs for cells_body
typically specify columns
and rows
.
OK, so columns = starts_with('outcome')
allows me limit the locations to the outcome columns, right?
Then, it's a matter of selecting WHICH rows in those columns?
I used rows = contains('bad')
because the docs say I can use tidyselect selection helpers and that seems plausible.
Obviously, I am missing something. What's going on?