Now let's say I will need to set red background color only if content is true for columns res1 and res2, if it's other values such as false or NA, just keep as original, which means I hope to highlight true cells for these two columns. How could I do that? Thanks.
References:
EDIT In case of multiple columns you could basically create the conditions like so. Basic idea it to first create a string containing the condition on which rows to target and using !!rlang::parse_expr to evaluate that string inside gt::cell_body:
Note: I added two more columns to your df to make the example more realistic.
library(gt)
library(magrittr)
df <- data.frame(id, res1, res2, res3 = res1, res4 = res2)
cols <- paste0("res", 1:4)
conditions <- lapply(cols, function(x) "true")
names(conditions) <- cols
rows <- paste(glue::glue("{.col} == '{.value}'", .col = names(conditions), .value = conditions), collapse = " & ")
df %>%
gt() %>%
tab_style(
style = list(
cell_fill(color = "red")
),
locations = cells_body(
columns = all_of(cols),
rows = !!rlang::parse_expr(rows)
)
)



