jthomasmock / gtExtras

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

gt_fa_rank_change() fails if col contains NAs and `show_text = FALSE` #80

Closed moodymudskipper closed 1 year ago

moodymudskipper commented 1 year ago

With CRAN and dev version

Taking the example from the doc and adding a NA and show_text = FALSE:

library(gtExtras)
dplyr::tibble(x = c(1:3, -1, -2, NA, -5, 0)) %>% 
  gt::gt() %>%
  gt_fa_rank_change(x, font_color = "match", show_text = FALSE)
#> Warning in fa_tbl$name == canonical_name: longer object length is not a multiple
#> of shorter object length
#> Error: The `name` provided ('NA') does not correspond to a known icon

I expect the NA row to be displayed the same whether show_text is TRUE or FALSE

Created on 2023-02-16 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.1 (2022-06-23) #> os macOS Monterey 12.0.1 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Zurich #> date 2023-02-16 #> pandoc 2.19.2 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.0 2023-01-09 [1] CRAN (R 4.2.0) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.2.0) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.0) #> dplyr 1.1.0.9000 2023-02-16 [1] Github (tidyverse/dplyr@2f2bd2a) #> evaluate 0.17 2022-10-07 [1] CRAN (R 4.2.0) #> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> fontawesome 0.5.0 2023-01-25 [1] CRAN (R 4.2.0) #> fs 1.6.1 2023-02-06 [1] CRAN (R 4.2.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.0) #> ggplot2 3.4.1 2023-02-10 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> gt 0.8.0 2022-11-16 [1] CRAN (R 4.2.0) #> gtable 0.3.1 2022-09-01 [1] CRAN (R 4.2.0) #> gtExtras * 0.4.6 2023-02-16 [1] Github (jthomasmock/gtExtras@1f21f72) #> highr 0.9 2021-04-16 [1] CRAN (R 4.2.0) #> htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.2.0) #> knitr 1.40 2022-08-24 [1] CRAN (R 4.2.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.0) #> paletteer 1.5.0 2022-10-19 [1] CRAN (R 4.2.0) #> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.1.1) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> purrr 0.3.5 2022-10-06 [1] CRAN (R 4.2.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.1.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.1.1) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.1.1) #> R.utils 2.12.0 2022-06-28 [1] CRAN (R 4.1.1) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> rematch2 2.1.2 2020-05-01 [1] CRAN (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.0) #> rlang 1.0.6.9000 2023-02-16 [1] Github (r-lib/rlang@5037f3a) #> rmarkdown 2.20 2023-01-19 [1] CRAN (R 4.2.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.0) #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> stringi 1.7.8 2022-07-11 [1] CRAN (R 4.2.0) #> stringr 1.4.1.9000 2022-11-07 [1] Github (tidyverse/stringr@f482fb0) #> styler 1.8.0 2022-10-22 [1] CRAN (R 4.2.0) #> tibble 3.1.8 2022-07-22 [1] CRAN (R 4.2.0) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.1) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.2.0) #> vctrs 0.5.2.9000 2023-02-16 [1] Github (r-lib/vctrs@303b5dd) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.36 2022-12-21 [1] CRAN (R 4.2.0) #> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.2.0) #> #> [1] /Users/Antoine/Library/R/arm64/4.2/library #> [2] /opt/R/4.2.1-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
jthomasmock commented 1 year ago

Thanks for reprex! I've addressed this in the latest commit.

library(gtExtras)
library(gt)

base_tab <- dplyr::tibble(x = c(1:3, -1, -2, NA, -5, 0)) %>% 
  gt::gt() 

tab_no_txt <- base_tab %>%
  gt_fa_rank_change(x, font_color = "match", show_text = FALSE)

gtsave(tab_no_txt, "notext.png")
knitr::include_graphics("notext.png")

tab_txt <- base_tab %>%
  gt_fa_rank_change(x, font_color = "match", show_text = TRUE)

gtsave(tab_txt, "text.png")
knitr::include_graphics("text.png")

Created on 2023-02-19 by the reprex package (v2.0.1)