jthomasmock / gtExtras

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

Potential `gt_fa_column` bug #85

Closed r-drigo closed 1 year ago

r-drigo commented 1 year ago

Hi

I have a table with multiple columns with strings to be turned into FontAwesome icons. The function gt_fa_column works just great for a single column. My code sets all columns inside a loop, one by one, selecting the proper number of colors to the palette. But in some cases it complains:

"Error in FUN(X[[i]], ...) : The length of the unique elements must match the palette length".

I can't find a pattern for that. Usually, when I have 6 columns, the loop deals with all of them, and crashes in the 6th execution, even though I assure the palette length is correct.

The code below is similar to the one I'm using. I have no clue why it crashes only when I have 6 of such columns, so just in case that's what I mimic here. Please note how the first loop generates an error, and the second one does not, even though it is basically similar to the concept of gt_fa_column as far as I understand.


test_tibble <- tibble::tibble(
  col_1 = sample(1:5, 3),
  icon_1 = sample(icon_list, 3, replace = TRUE),
  col_2 = sample(1:5, 3),
  icon_2 = sample(icon_list, 3, replace = TRUE),
  col_3 = sample(1:5, 3),
  icon_3 = sample(icon_list, 3, replace = TRUE),
  col_4 = sample(1:5, 3),
  icon_4 = sample(icon_list, 3, replace = TRUE),
  col_5 = sample(1:5, 3),
  icon_5 = sample(icon_list, 3, replace = TRUE),
  col_6 = sample(1:5, 3),
  icon_6 = sample(icon_list, 3, replace = TRUE)
) 

icons_pallete <- c(
  "circle" = "black",
  "check" = "blue",
  "square" = "white",
  "arrow-up" = "gold",
  "arrow-down" = "green"
)

# Doesn't work!
test_tibble_gt <- test_tibble %>% 
  gt()
for(i in 1:6){
  icon_colname <- paste0("icon_", i)
  icons_used <- test_tibble %>% 
    pull(!!sym(icon_colname)) %>% 
    unique()
  icons_pallete_used <- icons_pallete[icons_used]
  test_tibble_gt <- test_tibble_gt %>% 
    gtExtras::gt_fa_column(paste0("icon_", i),
                           palette = icons_pallete_used,
                           height = "1em")
}
test_tibble_gt

# Works fine!
test_tibble_gt <- test_tibble %>% 
  gt()
for(i in 1:6){
  icon_colname <- paste0("icon_", i)
  test_tibble_gt <- test_tibble_gt %>%
    text_transform(
      locations = cells_body(columns = icon_colname),
      fn = \(x){
        icon <- \(x) html(fontawesome::fa(x, fill = unname(icons_pallete[x])))
        purrr::map(x, icon)
      }
    )
}
test_tibble_gt```

Maybe I am doing something wrong, but anyway I hope it helps in some way. Thanks!
jthomasmock commented 1 year ago

Howdy! I modified/simplified your reprex, and I guessed icon_list was:

icon_list <- c("circle", "check", "square", "arrow-up", "arrow-down")

But I am also not sure I am following completely. Are you looping to apply the function to each column?

You could simplify the call to:

test_tibble_gt |> 
  gt_fa_column(
    contains("icon"),
    palette = icons_pallete
  )

Regardless, I've removed that check (palette unique length) for now as it's been finicky. Try the latest dev version here on GH.

library(gtExtras)
#> Loading required package: gt
library(dplyr)

icon_list <- c("circle", "check", "square", "arrow-up", "arrow-down")

set.seed(20230316)

test_tibble <- tibble::tibble(
  col_1 = sample(1:5, 3),
  icon_1 = sample(icon_list, 3, replace = TRUE),
  col_2 = sample(1:5, 3),
  icon_2 = sample(icon_list, 3, replace = TRUE),
  col_3 = sample(1:5, 3),
  icon_3 = sample(icon_list, 3, replace = TRUE),
  col_4 = sample(1:5, 3),
  icon_4 = sample(icon_list, 3, replace = TRUE),
  col_5 = sample(1:5, 3),
  icon_5 = sample(icon_list, 3, replace = TRUE),
  col_6 = sample(1:5, 3),
  icon_6 = sample(icon_list, 3, replace = TRUE)
) 

icons_pallete <- c(
  "circle" = "black",
  "check" = "blue",
  "square" = "red",
  "arrow-up" = "gold",
  "arrow-down" = "green"
)

test_tibble_gt <- test_tibble %>% 
  gt()

test_tibble_gt |> 
  gt_fa_column(
    contains("icon"),
    palette = icons_pallete
  ) |> 
  gt_reprex_image()

Created on 2023-03-16 by the reprex package (v2.0.1)