GuangchuangYu / ggplotify

ggplot everything
https://cran.r-project.org/package=ggplotify/vignettes/ggplotify.html
105 stars 11 forks source link

as.ggplot always seems to look up the global environment #6

Closed sinnhazime closed 5 years ago

sinnhazime commented 5 years ago

I tried to use ggplotify::as.ggplot in a function, but I couldn't.

not_work <- function(not_global_x = c(1,2), not_global_y = c(2,3)) { ggplotify::as.ggplot(~ plot(not_global_x, not_global_y)) }

The error message was "Error: object 'not_global_x' not found". When I assigned the valiables used in my formula to the global environment, it worked.

do_work <- function(not_global_x = c(1,2), not_global_y = c(2,3)) { assign("not_global_x", not_global_x, globalenv()) assign("not_global_y", not_global_x, globalenv()) ggplotify::as.ggplot(~ plot(not_global_x, not_global_y)) rm(list = c("not_global_x", "not_global_y"), envir = globalenv()) }

I think it would be nice if as.ggplot looked up the environment where as.ggplot was called.

SessionInfo: R version 3.5.0 (2018-04-23) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 18.04.3 LTS

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

other attached packages: [1] ggplotify_0.0.4

loaded via a namespace (and not attached): [1] Rcpp_1.0.1 rstudioapi_0.10 magrittr_1.5 tidyselect_0.2.5 munsell_0.5.0 colorspace_1.4-1
[7] R6_2.4.0 rlang_0.4.0 dplyr_0.8.3.9000 tools_3.5.0 grid_3.5.0 packrat_0.5.0
[13] gtable_0.3.0 lambda.r_1.2.3 futile.logger_1.4.3 gridGraphics_0.4-1 lazyeval_0.2.2 assertthat_0.2.1
[19] tibble_2.1.3 crayon_1.3.4 formatR_1.6 purrr_0.3.2 ggplot2_3.2.0 futile.options_1.0.1 [25] glue_1.3.1 VennDiagram_1.6.20 compiler_3.5.0 pillar_1.4.2 rvcheck_0.1.3 scales_1.0.0
[31] pkgconfig_2.0.2

GuangchuangYu commented 5 years ago

do_work <- function(not_global_x = c(1,2), not_global_y = c(2,3)) { ggplotify::as.ggplot(function() plot(not_global_x, not_global_y)) }

sinnhazime commented 5 years ago

Thank you for your help. It's my (silly) overlooking.