library(gt)
library(dplyr)
data <- 1:150 %>% sample(10) %>% iris[.,]
data
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
68 5.8 2.7 4.1 1.0 versicolor
102 5.8 2.7 5.1 1.9 virginica
61 5.0 2.0 3.5 1.0 versicolor
81 5.5 2.4 3.8 1.1 versicolor
49 5.3 3.7 1.5 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
117 6.5 3.0 5.5 1.8 virginica
26 5.0 3.0 1.6 0.2 setosa
142 6.9 3.1 5.1 2.3 virginica
91 5.5 2.6 4.4 1.2 versicolor
What I want to achieve is something like this:
data %>% arrange(Species) %>% gt() %>%
tab_style(
style = cell_fill(color = '#F0FDEC'),
locations = cells_body(
rows = Species == 'setosa')) %>%
tab_style(
style = cell_fill(color = '#F0FDEC'),
locations = cells_body(
rows = Species == 'virginica'))
I don't want to manually specify which Species should be colourd so that the code can scale.
Thank you in advance!
CodePudding user response:
You can convert to numeric
(and factor
before if it's not already the case) and set TRUE
for every uneven group ids:
data %>%
arrange(Species) %>%
gt() %>%
tab_style(
style = cell_fill(color = '#F0FDEC'),
locations = cells_body(
rows = as.numeric(Species) %% 2 == 1))
CodePudding user response:
Do you mean this?
library(gt)
library(dplyr)
iris %>% arrange(Species) %>% gt() %>%
tab_style(
style = cell_fill(color = '#F0FDEC'),
locations = cells_body(
rows = Species != 'versicolor'))