'Conditional formatting of multiple columns in gt table
This is my first post, so apologies if I messed something up. I am trying to apply conditional formatting to multiple columns (comparing results for samples SampA, SampB and SampB to Limit) of a gt table. Following the lead of one of the gt examples and a different stack overflow Q, I have managed to apply it to a single column (variable) using this code:
## Conditional Formatting of single column in gt table
samples = as_tibble(cbind("Chem"=c("Cd","Pb","Zn"),
"Limit"=c("0.005","0.05","0.007"),
"SampA" = c("0.001","0.15","0.003"),
"SampB" = c("0.002","0.04","0.005"),
"SampC" = c("0.009","0.23","0.03")))
gt(samples,rowname_col="Chem") %>% tab_style(
style = list(
cell_fill(color = "grey80"),
cell_text(weight = "bold")
),
locations = cells_body(
columns = vars(SampA),
rows = SampA >= Limit
)
) %>% tab_spanner(
label = "Samples",
columns = vars(SampA,SampB,SampC))
However, I have not been successful in trying to expand this to multiple columns. I can get the 'columns' argument to work with 'vars(SampA,SampB,SampC)'. Leaving the 'rows' argument with 'SampA >= Limit', the formatting 'works' in the sense that all rows where SampA >= Limit are highlighted across the three Samp columns, but this is not what I'm after. Running the code below ends up with no formatting of any column.
locations = cells_body(
columns = vars(SampA,SampB,SampC),
rows = vars(SampA,SampB,SampC) >= Limit
)
I have been able to "brute force" what I'm after by repeating the 'style_tab()' tailored to each column, but know that there must be a better way to get to my goal. Help?
Solution 1:[1]
I know this is probably too late to help you, but I was able to figure it out by defining a function that creates a list of cells_body calls with the correct parameters. This list is then passed to the locations parameter and applies the specified formatting to all the selected cells. Hopefully someone else finds this useful!
library(gt)
samples = as_tibble(cbind("Chem"=c("Cd","Pb","Zn"),
"Limit"=c("0.005","0.05","0.007"),
"SampA" = c("0.001","0.15","0.003"),
"SampB" = c("0.002","0.04","0.005"),
"SampC" = c("0.009","0.23","0.03")))
builder <- function(x, Limit){cells_body(columns = !!sym(x), rows = !!sym(x) > Limit)}
gt(samples,rowname_col="Chem") %>%
tab_style(style = list(cell_fill(color = "grey80"), cell_text(weight = "bold")),
locations = lapply(c("SampA", "SampB", "SampC"), test_fun, Limit = sym(Limit))) %>%
tab_spanner(label = "Samples", columns = c(SampA, SampB, SampC))
This will work with any vector of strings that match your column names, including what can be created through the colnames()
function
names <- colnames(samples)[3:ncol(samples)]
gt(samples,rowname_col="Chem") %>%
tab_style(style = list(cell_fill(color = "grey80"), cell_text(weight = "bold")),
locations = lapply(names, builder, Limit = sym(Limit))) %>%
tab_spanner(label = "Samples", columns = c(SampA, SampB, SampC))
Solution 2:[2]
library(gt)
samples = as_tibble(cbind("Chem"=c("Cd","Pb","Zn"),
"Limit"=c("0.005","0.05","0.007"),
"SampA" = c("0.001","0.15","0.003"),
"SampB" = c("0.002","0.04","0.005"),
"SampC" = c("0.009","0.23","0.03")))
samples <- samples %>%
mutate(Limit = as.numeric(Limit))
gt(samples,
rowname_col="Chem") %>%
tab_style(style = list(cell_fill(color = 'yellow'),
cell_text(weight = 'bold')),
locations = cells_body(columns=vars(SampA),
rows = SampA >= Limit)) %>%
tab_style(style = list(cell_fill(color = 'yellow'),
cell_text(weight = 'bold')),
locations = cells_body(columns=vars(SampB),
rows = SampB >= Limit)) %>%
tab_style(style = list(cell_fill(color = 'yellow'),
cell_text(weight = 'bold')),
locations = cells_body(columns=vars(SampC),
rows = SampC >= Limit)) %>%
tab_spanner(
label = "Samples",
columns = vars(SampA,SampB,SampC))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Susan Switzer |