-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to set background colors for the cells of multiple columns based on string contents #863
Comments
We need an easier way to set basic style attributes based on values. I think a new function for this should be the way to do it. The |
Thank you very much, I believe this function will have lots of application scenarios. |
Tagging on to this, there are a few requests I've seen on Twitter and I have a prototype that works like below: set.seed(37)
base_tab <- head(mtcars) |>
dplyr::slice_sample(n = 6) |>
dplyr::arrange(desc(mpg)) |>
gt::gt()
base_tab |>
gt_color_by_col(mpg, cyl, domain = range(gtExtras::gt_index(base_tab, mpg))) Essentially it loops through the column, values, and rows to apply a fn_data_clr_by_col <- function(gt_object, col2, value, row_id, color_add){
gt_object |>
tab_style(
style = list(
cell_fill(color = color_add),
cell_text(color = gt:::ideal_fgnd_color(color_add))
),
locations = cells_body({{ col2 }}, rows = row_id)
)
} for (i in 1:length(full_vals)){
gt_object <- fn_data_clr_by_col(gt_object, {{ col2 }}, full_vals[i], i, color_add[i])
}
return(gt_object) Full code: Full Codefn_data_clr_by_col <- function(gt_object, col2, value, row_id, color_add){
gt_object |>
tab_style(
style = list(
cell_fill(color = color_add),
cell_text(color = gt:::ideal_fgnd_color(color_add))
),
locations = cells_body({{ col2 }}, rows = row_id)
)
}
gt_color_by_col <- function(
gt_object, col1, col2, ..., domain = NULL, direction = 1, type = "continuous",
palette = c("#af8dc3", "#e7d4e8", "#f7f7f7", "#d9f0d3", "#7fbf7b")){
stopifnot("Table must be of class 'gt_tbl'" = "gt_tbl" %in% class(gt_object))
full_vals <- gtExtras::gt_index(gt_object = gt_object, {{ col1 }})
rng_vals <- range(full_vals, na.rm = TRUE)
if(is.null(domain)){
domain <- rng_vals
warning(
"Domain not specified, defaulting to observed range within each specified column.",
call. = FALSE
)
}
color_add <- scales::col_numeric(
palette = if(grepl(x = palette[1], pattern = "::")){
paletteer::paletteer_d(
palette = palette,
direction = direction,
type = pal_type
) %>% as.character()
} else {
if(direction == -1){
rev(palette)
} else {
palette
}
},
...,
domain = domain
)(full_vals)
for (i in 1:length(full_vals)){
gt_object <- fn_data_clr_by_col(gt_object, {{ col2 }}, full_vals[i], i, color_add[i])
}
return(gt_object)
}
|
@jthomasmock if it doesn't already exist could you transfer your comments/ideas to a new issue? There's some great stuff there and it's such a common problem that I don't want to lose track of this. |
Done! I've moved it to: #1119 with some of the public requests I've seen |
Hello, say I have a small dataset as follows:
For columns
res1
andres2
, let's say if value content aretrue
s, I'll need to highlight that cell with red background color, iffalse
s, highlight with green color, for other cases, keep their colors as original.Is it possible to achieve that using gt package?
The text was updated successfully, but these errors were encountered: