jthomasmock / gtExtras

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

gt_fa_column across multiple columns - unexpected behavior in (maybe?) common use case. #69

Closed mikedolanfliss closed 1 year ago

mikedolanfliss commented 1 year ago

Prework

Following this closed issue on gt_fa_column, I was hoping to color all the true/false -> icon columns in a dataset at once. I thought the below might work. After digging into the function, it seems that every mentioned column, even if coded at once, is checked as having the same number of unique values. Note that this may make sense (maybe?) for factors where every column is expected to have a different set of allowable levels. But even then, a factor may have multiple allowable levels, but not all represented in data. I would gt_fa_column to be ok with a palette representing the superset of unique values in the data. Here's a reprex and a few attempted - failed - workarounds.

Reproducible example

test_tbl = tibble(col1 = rep(T, 3), col2 = c(T, T, F), col3 = rep(F, 3))
test_tbl %>% 
  mutate(across(is_logical, ~ case_when(.x ~ "square-check", !.x ~ "square-xmark"))) %>% 
  gt

test_tbl %>% 
  mutate(across(is_logical, ~ case_when(.x ~ "square-check", !.x ~ "square-xmark"))) %>% 
  gt %>%
  gt_fa_column(column = col2, palette = c("square-check" = "green", "square-xmark" = "red"))

test_tbl %>% 
  mutate(across(is_logical, ~ case_when(.x ~ "square-check", !.x ~ "square-xmark"))) %>% 
  gt %>%
  gt_fa_column(column = col1:col3, palette = c("square-check" = "green", "square-xmark" = "red"))

I would have expected at least one of these attempts to iconify and color multiple columns to work.

Session info dump

R version 4.2.1 (2022-06-23 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

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

other attached packages: [1] googlesheets4_1.0.1 scales_1.2.1 gtExtras_0.4.2 gtsummary_1.6.1 gt_0.7.0 assertthat_0.2.1 lubridate_1.8.0 RSocrata_1.7.11-2 forcats_0.5.2
[10] stringr_1.4.1 dplyr_1.0.10 purrr_0.3.4 readr_2.1.2 tidyr_1.2.1 tibble_3.1.8 ggplot2_3.3.6 tidyverse_1.3.2

loaded via a namespace (and not attached): [1] httr_1.4.4 sass_0.4.2 bit64_4.0.5 vroom_1.5.7 jsonlite_1.8.0 viridisLite_0.4.1 modelr_0.1.9 paletteer_1.4.1 cellranger_1.1.0
[10] yaml_2.3.5 pillar_1.8.1 backports_1.4.1 glue_1.6.2 digest_0.6.29 RColorBrewer_1.1-3 rvest_1.0.3 colorspace_2.0-3 htmltools_0.5.3
[19] plyr_1.8.7 pkgconfig_2.0.3 broom_1.0.1 haven_2.5.1 fontawesome_0.3.0 tzdb_0.3.0 googledrive_2.0.0 generics_0.1.3 farver_2.1.1
[28] ellipsis_0.3.2 withr_2.5.0 cli_3.4.1 magrittr_2.0.3 crayon_1.5.1 readxl_1.4.1 mime_0.12 evaluate_0.16 fs_1.5.2
[37] fansi_1.0.3 broom.helpers_1.9.0 xml2_1.3.3 tools_4.2.1 hms_1.1.2 gargle_1.2.1 lifecycle_1.0.2 munsell_0.5.0 reprex_2.0.2
[46] compiler_4.2.1 rlang_1.0.6 grid_4.2.1 rstudioapi_0.14 rmarkdown_2.16 gtable_0.3.1 DBI_1.1.3 curl_4.3.2 rematch2_2.1.2
[55] R6_2.5.1 knitr_1.40 fastmap_1.1.0 bit_4.0.4 utf8_1.2.2 stringi_1.7.8 parallel_4.2.1 Rcpp_1.0.9 vctrs_0.4.1
[64] dbplyr_2.2.1 tidyselect_1.1.2 xfun_0.33

jthomasmock commented 1 year ago

Howdy! Most of the gt_fa_??? columns are intended to apply across a single column at a time - there are some simple situations where they can apply across multiple columns, but they are definitely not written to merge data across columns but rather treat each column as it's own thing.

I'm not likely to do a major rewrite of the internals to be robust across columns currently, but will keep it in mind in the future.

You could accomplish your intended output, column by column. This relies on reducing the "palette" matching the unique values within the column.

Going to close with the reprex below but let me know if you have further questions.

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

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

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, palette = ex_pal[gt_index(test_tab, col1)] %>% unique()) %>%
  gt_fa_column(column = col2, palette = ex_pal[gt_index(test_tab, col2)] %>% unique()) %>%
  gt_fa_column(column = col3, palette = ex_pal[gt_index(test_tab, col3)] %>% unique()) %>%
  gtsave("test-img.png")

knitr::include_graphics("test-img.png")

Created on 2022-10-02 by the reprex package (v2.0.1)