Skip to content
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

fill_color(columns, domain) reuses colors (two examples) #1877

Open
6 tasks done
aronatkins opened this issue Sep 11, 2024 · 0 comments
Open
6 tasks done

fill_color(columns, domain) reuses colors (two examples) #1877

aronatkins opened this issue Sep 11, 2024 · 0 comments

Comments

@aronatkins
Copy link

Prework

Description

gt::data_color() reuses colors across cell values.

Reproducible example

  • Post a minimal reproducible example so the maintainer can troubleshoot the problems you identify. A reproducible example is:
    • Runnable: post enough R code and data so any onlooker can create the error on their own computer.
    • Minimal: reduce runtime wherever possible and remove complicated details that are irrelevant to the issue at hand.
    • Readable: format your code according to the tidyverse style guide.

This example contains two tables. They have the same data, but presented differently. Both tables want to color the cells (other than the first column). They compute a set of values (the same in both tables) and use that as the color domain.

people <- data.frame(
  "Person" = c("Alice", "Bob", "Carl"),
  "2024-09-09" = c("Alpha", "Alpha", "Kappa"),
  "2024-09-16" = c("Alpha", "Alpha", "Kappa"),
  "2024-09-23" = c("Alpha", "Alpha",NA),
  "2024-09-30" = c("Fill", "Wilco", "Wilco"),
  "2024-10-07" = c(NA,"Wilco", "Wilco"),
  "2024-10-14" = c("Beta", "Beta",NA),
  "2024-10-21" = c("Beta", "Beta",NA),
  "2024-10-28" = c("Beta", "Beta",NA),
  stringsAsFactors = FALSE
)

pcolumns <- colnames(people)
pcolumns <- pcolumns[pcolumns != "Person"]
pvalues <- unique(c(unlist(lapply(pcolumns, function(v) { people[[v]] }))))
pvalues <- sort(pvalues, na.last=TRUE)

people |>
  gt::gt() |>
  gt::opt_row_striping(FALSE) |>
  gt::data_color(columns=pcolumns, domain=pvalues, na_color="#cdcdcd")

dates <- data.frame(
  "Date" = c("2024-09-09", "2024-09-16", "2024-09-23", "2024-09-30", "2024-10-07", "2024-10-14", "2024-10-21", "2024-10-28"),
  "Alice" = c("Alpha", "Alpha", "Alpha", "Fill", NA, "Beta", "Beta", "Beta"),
  "Bob" = c("Alpha", "Alpha", "Alpha", "Wilco", "Wilco", "Beta", "Beta", "Beta"),
  "Carl" = c("Kappa", "Kappa", NA, "Wilco", "Wilco", NA, NA, NA),
  stringsAsFactors = FALSE
)

dcolumns <- colnames(dates)
dcolumns <- dcolumns[dcolumns != "Date"]
dvalues <- unique(c(unlist(lapply(dcolumns, function(v) { dates[[v]] }))))
dvalues <- sort(dvalues, na.last=TRUE)

dates |>
  gt::gt() |>
  gt::opt_row_striping(FALSE) |>
  gt::data_color(columns=dcolumns, domain=dvalues, na_color="#cdcdcd")

The first table renders as:

image

The second table renders as:

image

Expected result

All cells with the same value should use the same color. No cells with a different value should share color.

These tables were produced using a similar approach with the Python Great Tables package and show better cell coloring.

image image

Session info

End the reproducible example with a call to sessionInfo() in the same session (e.g. reprex(session_info = TRUE)) and include the output.

R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.6.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] bit_4.0.5          jsonlite_1.8.8     dplyr_1.1.4        compiler_4.3.3    
 [5] renv_1.0.7         crayon_1.5.3       tidyselect_1.2.1   xml2_1.3.6        
 [9] parallel_4.3.3     scales_1.3.0       yaml_2.3.10        fastmap_1.2.0     
[13] readr_2.1.5        R6_2.5.1           generics_0.1.3     curl_5.2.2        
[17] PKI_0.1-14         tibble_3.2.1       munsell_0.5.1      openssl_2.2.0     
[21] pillar_1.9.0       RColorBrewer_1.1-3 tzdb_0.4.0         rlang_1.1.4       
[25] utf8_1.2.4         sass_0.4.9         bit64_4.0.5        cli_3.6.3         
[29] withr_3.0.1        magrittr_2.0.3     digest_0.6.37      vroom_1.6.5       
[33] base64enc_0.1-3    hms_1.1.3          askpass_1.2.0      lifecycle_1.0.4   
[37] vctrs_0.6.5        glue_1.7.0         farver_2.1.2       rsconnect_1.3.1   
[41] gt_0.11.0          fansi_1.0.6        colorspace_2.1-1   tools_4.3.3       
[45] pkgconfig_2.0.3    htmltools_0.5.8.1 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Backlog
Development

No branches or pull requests

2 participants