gt icon indicating copy to clipboard operation
gt copied to clipboard

Using paletteer in data_color has name conflicts

Open jthomasmock opened this issue 3 years ago • 0 comments

Description

When using paletteer inside data_color() there is a potential for name conflict, specifically when type is a column within the underlying dataframe it is interpreted within the context of the dataframe inside paletteer functions that AFAIK shouldn't be accessing/referncing the underlying data. As discovered in gtExtras Issue 50

Reproducible example

library(gt)

base_gt <- dplyr::tibble(
  "name" = c("A", "B", "C"),
  type = c("a", "b", "c"), 
  value = c(0.1, 0.6, 0.9)
) %>%
  gt()

# type is not present in the global env
type
#> Error in eval(expr, envir, enclos): object 'type' not found

# evaluates type from the underlying dataframe
base_gt %>% 
  data_color(
    columns = value,
    colors = scales::col_numeric(
      palette = 
        paletteer::paletteer_d(
          palette = "ggsci::red_material",
          type = type
        ) %>% as.character(),
      domain = c(0,1)
    )
  )
#> Error in match.arg(type): 'arg' must be of length 1

# allows for operations on type, where type == "a"
# so it throws an error not matching discrete or continuous

base_gt %>% 
  data_color(
    columns = value,
    colors = scales::col_numeric(
      palette = 
        paletteer::paletteer_d(
          palette = "ggsci::red_material",
          type = type[1]
        ) %>% as.character(),
      domain = c(0,1)
    )
  )
#> Error in match.arg(type): 'arg' should be one of "discrete", "continuous"

Created on 2022-05-23 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.0 (2022-04-22)
#>  os       macOS Monterey 12.2.1
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Chicago
#>  date     2022-05-23
#>  pandoc   2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.2.0)
#>  backports     1.4.1      2021-12-13 [1] CRAN (R 4.2.0)
#>  checkmate     2.1.0      2022-04-21 [1] CRAN (R 4.2.0)
#>  cli           3.3.0      2022-04-25 [1] CRAN (R 4.2.0)
#>  colorspace    2.0-3      2022-02-21 [1] CRAN (R 4.2.0)
#>  crayon        1.5.1      2022-03-26 [1] CRAN (R 4.2.0)
#>  DBI           1.1.2      2021-12-20 [1] CRAN (R 4.2.0)
#>  digest        0.6.29     2021-12-01 [1] CRAN (R 4.2.0)
#>  dplyr         1.0.8      2022-02-08 [1] CRAN (R 4.2.0)
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.2.0)
#>  evaluate      0.15       2022-02-18 [1] CRAN (R 4.2.0)
#>  fansi         1.0.3      2022-03-24 [1] CRAN (R 4.2.0)
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.2.0)
#>  fs            1.5.2      2021-12-08 [1] CRAN (R 4.2.0)
#>  generics      0.1.2      2022-01-31 [1] CRAN (R 4.2.0)
#>  ggplot2       3.3.5      2021-06-25 [1] CRAN (R 4.2.0)
#>  glue          1.6.2      2022-02-24 [1] CRAN (R 4.2.0)
#>  gt          * 0.5.0.9000 2022-05-20 [1] Github (rstudio/gt@e6a762f)
#>  gtable        0.3.0      2019-03-25 [1] CRAN (R 4.2.0)
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.2.0)
#>  htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.2.0)
#>  knitr         1.38.3     2022-04-26 [1] Github (yihui/knitr@87e5d8e)
#>  lifecycle     1.0.1      2021-09-24 [1] CRAN (R 4.2.0)
#>  magrittr      2.0.2      2022-01-26 [1] CRAN (R 4.2.0)
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 4.2.0)
#>  paletteer     1.4.0      2021-07-20 [1] CRAN (R 4.2.0)
#>  pillar        1.7.0      2022-02-01 [1] CRAN (R 4.2.0)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.2.0)
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.2.0)
#>  R.cache       0.15.0     2021-04-30 [1] CRAN (R 4.2.0)
#>  R.methodsS3   1.8.1      2020-08-26 [1] CRAN (R 4.2.0)
#>  R.oo          1.24.0     2020-08-26 [1] CRAN (R 4.2.0)
#>  R.utils       2.11.0     2021-09-26 [1] CRAN (R 4.2.0)
#>  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.1      2021-08-05 [1] CRAN (R 4.2.0)
#>  rlang         1.0.2      2022-03-04 [1] CRAN (R 4.2.0)
#>  rmarkdown     2.14       2022-04-25 [1] CRAN (R 4.2.0)
#>  rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.2.0)
#>  scales        1.2.0      2022-04-13 [1] CRAN (R 4.2.0)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.2.0)
#>  stringi       1.7.6      2021-11-29 [1] CRAN (R 4.2.0)
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.2.0)
#>  styler        1.7.0      2022-03-13 [1] CRAN (R 4.2.0)
#>  tibble        3.1.6      2021-11-07 [1] CRAN (R 4.2.0)
#>  tidyselect    1.1.2      2022-02-21 [1] CRAN (R 4.2.0)
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.2.0)
#>  vctrs         0.4.1      2022-04-13 [1] CRAN (R 4.2.0)
#>  withr         2.5.0      2022-03-03 [1] CRAN (R 4.2.0)
#>  xfun          0.30       2022-03-02 [1] CRAN (R 4.2.0)
#>  yaml          2.3.5      2022-02-21 [1] CRAN (R 4.2.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Expected result

I would expect type to not be evaluated within the context/environment of the underlying dataframe.

jthomasmock avatar May 23 '22 14:05 jthomasmock