hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
322 stars 28 forks source link

by_cases does not pick outer free variable #252

Closed blset closed 11 months ago

blset commented 11 months ago

Hello,

with latest huxtable

using a construct like


  far_mean <- by_cases(
    . < 0 ~ "firebrick",
    . < botmean ~ "springgreen3",
    . > topmean ~ "springgreen4",
    TRUE ~ "black"
    )

 tab <- map_text_color(tab, -1, final(1), far_mean)

with botmean and topmean defined in the enclosing environment I get and error from dplyr::case_when


Erreur dans dplyr::case_when(!!!cases) : 
  Failed to evaluate the left-hand side of formula 2.
Caused by error:
! objet 'botmean' introuvable
hughjonesd commented 11 months ago

Please could you provide a minimal self-contained example?

blset commented 11 months ago
library(huxtable)
htexample <- function(data) {

  tab = huxtable(data, add_colnames = FALSE)
  tab <- tab |>
    dplyr::mutate(
      x = (tab[["colc"]] - tab[["colp"]]) / tab[["colp"]]
    )

  evolmean <- tab[[nrow(tab), "x"]] |> as.numeric()

  tab <- add_colnames(tab)

  far_mean <- by_cases(
    . < 0 ~ "firebrick",
    . < evolmean / 1.1 ~ "springgreen3",
    . > evolmean * 1.1 ~ "springgreen4",
    TRUE ~ "black"
  )

  # formatage pct importants
  tab <- map_text_color(tab, -(1), final(1), far_mean)

}

t1 = data.frame(

  seg=letters[1:5],
  colp = 12:16,
  colc = 20:24
)

htexample(t1)

result of running after restarting R, the problem is with dplyr

the env did not pick up the evolmean variable

Error in `dplyr::case_when()`:
! Failed to evaluate the left-hand side of formula 2.
Caused by error:
! objet 'evolmean' introuvable
Run `rlang::last_trace()` to see where the error occurred.

R version 4.3.2 (2023-10-31) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.6 LTS

Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale: [1] LC_CTYPE=fr_FR.UTF-8 LC_NUMERIC=C
[3] LC_TIME=fr_FR.UTF-8 LC_COLLATE=fr_FR.UTF-8
[5] LC_MONETARY=fr_FR.UTF-8 LC_MESSAGES=fr_FR.UTF-8
[7] LC_PAPER=fr_FR.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C

time zone: Europe/Paris tzcode source: system (glibc)

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

other attached packages: [1] huxtable_5.5.3

loaded via a namespace (and not attached): [1] assertthat_0.2.1 utf8_1.2.3 R6_2.5.1
[4] tidyselect_1.2.0 xfun_0.39 magrittr_2.0.3
[7] glue_1.6.2 tibble_3.2.1 knitr_1.45
[10] pkgconfig_2.0.3 dplyr_1.1.2 generics_0.1.3
[13] lifecycle_1.0.3 cli_3.6.1 fansi_1.0.4
[16] vctrs_0.6.3 withr_2.5.0 compiler_4.3.2
[19] rstudioapi_0.15.0 tools_4.3.2 pillar_1.9.0
[22] rlang_1.1.2 crayon_1.5.2 stringi_1.7.12

hughjonesd commented 11 months ago

Nice, thanks. Please try github master, which should fix your bug.