jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
193 stars 26 forks source link

Feature request: Allow subset of palette & missing values in `gt_fa_column()` #78

Closed areckenrode closed 1 year ago

areckenrode commented 1 year ago

Prework

In search of combination of these two features:

Using the example from https://github.com/jthomasmock/gtExtras/issues/70#issuecomment-1264838427 , if we add a fourth column that contains NA, we can get 2 different results.

library(gt)
library(gtExtras)
library(tidyverse)

test_tbl = tibble(col1 = rep(T, 3), col2 = c(T, T, F), col3 = rep(F, 3), col4 = c(T, F, NA))

ex_pal <- c("square-check" = "green", "square-xmark" = "red")

test_tab <- test_tbl %>% 
  mutate(across(where(is_logical),~ case_when(.x ~ "square-check", !.x ~ "square-xmark"))) %>% 
  gt()

test_tab %>%
  gt_fa_column(column = col1:col4, palette = ex_pal)

image

library(gt)
library(gtExtras)
library(tidyverse)

test_tbl = tibble(col1 = rep(T, 3), col2 = c(T, T, F), col3 = rep(F, 3), col4 = c(T, T, NA))

ex_pal <- c("square-check" = "green", "square-xmark" = "red")

test_tab <- test_tbl %>% 
  mutate(across(where(is_logical),~ case_when(.x ~ "square-check", !.x ~ "square-xmark"))) %>% 
  gt()

test_tab %>%
  gt_fa_column(column = col1:col4, palette = ex_pal)
Error in FUN(X[[i]], ...) : 
  The length of the unique elements must match the palette length

Proposal

To share a palette for an element across multiple tables or columns by defining that palette, even if sometimes the individual table/column doesn't have the full list of factor values and has NAs.

The current version of the packages allows for:

  1. Entire palette to be used and NAs ignored
  2. Subset of palette to be used

I desire a third option:

  1. Subset of palette to be used and NAs ignored.

The gtExtras package is extremely cool and very useful. This has been a wonderful resource. I appreciate any guidance you may have on this topic.

jthomasmock commented 1 year ago

Howdy! I'll look into this, but generally I think that NAs are a mistake for the table in this case. By that I mean typically we would be pre-processing the input table, so we could either shift the NA to a meaningful value (ie change NA to "blank") or deal with it on the fly like below:

library(gt)
library(gtExtras)
library(tidyverse)

test_tbl = tibble(col1 = rep(T, 3), col2 = c(T, T, F), col3 = rep(F, 3), col4 = c(T, T, NA))

ex_pal <- c("square-check" = "green", "square-xmark" = "red", "square" = "lightgrey")

test_tab <- test_tbl %>% 
  mutate(across(where(is_logical),~ case_when(.x ~ "square-check", !.x ~ "square-xmark", is.na(.x) ~ "square"))) %>% 
  gt()

test_tab

test_tab %>%
  gt_fa_column(column = col1:col4, palette = ex_pal)
Screen Shot 2023-01-09 at 2 56 21 PM
areckenrode commented 1 year ago

Thank you for your response and suggestion.

I do apologize - I'd unsuccessfully tried out this option with an older version of this package & assumed it would continue to fail. However, with the newest update, this option does work!

I'd still be interested to see the 'Subset of palette to be used and NAs ignored', but your suggestion is a great option!