Last active
November 3, 2022 12:02
-
-
Save LukasWallrich/4a91e1831db76eb66d4c583ee371ba38 to your computer and use it in GitHub Desktop.
gt data_color extension to target column
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| my_data_color <- function (data, SOURCE_columns, TARGET_columns, colors, alpha = NULL, apply_to = c("fill", | |
| "text"), autocolor_text = TRUE) | |
| { | |
| stop_if_not_gt(data = data) | |
| apply_to <- match.arg(apply_to) | |
| colors <- rlang::enquo(colors) | |
| data_tbl <- dt_data_get(data = data) | |
| colors <- rlang::eval_tidy(colors, data_tbl) | |
| resolved_source_columns <- resolve_cols_c(expr = { | |
| { | |
| SOURCE_columns | |
| } | |
| }, data = data) | |
| resolved_target_columns <- resolve_cols_c(expr = { | |
| { | |
| TARGET_columns | |
| } | |
| }, data = data) | |
| rows <- seq_len(nrow(data_tbl)) | |
| data_color_styles_tbl <- dplyr::tibble(locname = character(0), | |
| grpname = character(0), colname = character(0), locnum = numeric(0), | |
| rownum = integer(0), colnum = integer(0), styles = list()) | |
| for (i in seq_along(resolved_source_columns)) { | |
| data_vals <- data_tbl[[resolved_source_columns[i]]][rows] | |
| if (inherits(colors, "character")) { | |
| if (is.numeric(data_vals)) { | |
| color_fn <- scales::col_numeric(palette = colors, | |
| domain = data_vals, alpha = TRUE) | |
| } | |
| else if (is.character(data_vals) || is.factor(data_vals)) { | |
| if (length(colors) > 1) { | |
| nlvl <- if (is.factor(data_vals)) { | |
| nlevels(data_vals) | |
| } | |
| else { | |
| nlevels(factor(data_vals)) | |
| } | |
| if (length(colors) > nlvl) { | |
| colors <- colors[seq_len(nlvl)] | |
| } | |
| } | |
| color_fn <- scales::col_factor(palette = colors, | |
| domain = data_vals, alpha = TRUE) | |
| } | |
| else { | |
| cli::cli_abort("Don't know how to map colors to a column of class {class(data_vals)[1]}.") | |
| } | |
| } | |
| else if (inherits(colors, "function")) { | |
| color_fn <- colors | |
| } | |
| else { | |
| cli::cli_abort("The `colors` arg must be either a character vector of colors or a function.") | |
| } | |
| color_fn <- rlang::eval_tidy(color_fn, data_tbl) | |
| color_vals <- color_fn(data_vals) | |
| color_vals <- html_color(colors = color_vals, alpha = alpha) | |
| color_styles <- switch(apply_to, fill = lapply(color_vals, | |
| FUN = function(x) cell_fill(color = x)), text = lapply(color_vals, | |
| FUN = function(x) cell_text(color = x))) | |
| data_color_styles_tbl <- dplyr::bind_rows(data_color_styles_tbl, | |
| generate_data_color_styles_tbl(column = resolved_target_columns[i], rows = rows, | |
| color_styles = color_styles)) | |
| if (apply_to == "fill" && autocolor_text) { | |
| color_vals <- ideal_fgnd_color(bgnd_color = color_vals) | |
| color_styles <- lapply(color_vals, FUN = function(x) cell_text(color = x)) | |
| data_color_styles_tbl <- dplyr::bind_rows(data_color_styles_tbl, | |
| generate_data_color_styles_tbl(column = resolved_target_columns[i], | |
| rows = rows, color_styles = color_styles)) | |
| } | |
| } | |
| dt_styles_set(data = data, styles = dplyr::bind_rows(dt_styles_get(data = data), | |
| data_color_styles_tbl)) | |
| } | |
| library(gt) | |
| # Add function into gt namespace (so that internal gt functions can be called) | |
| tmpfun <- get("data_color", envir = asNamespace("gt")) | |
| environment(my_data_color) <- environment(tmpfun) | |
| attributes(my_data_color) <- attributes(tmpfun) | |
| # Test | |
| data <- data.frame(cat = LETTERS[1:3], | |
| M_SD = c("1.1 (0.7)", "3.5 (0.6)", "2.0 (0.7)"), | |
| M = c(1.1, 3.5, 2.0)) | |
| # DESIRED code | |
| gt(data) %>% | |
| cols_hide(3) %>% | |
| my_data_color(SOURCE_columns = 3, TARGET_columns = 2, | |
| colors = scales::col_numeric(palette = c("red", "green"), domain = c(1, 4))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment