Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where should character globals be looked up? #62

Open
DavisVaughan opened this issue Jul 29, 2020 · 0 comments
Open

Where should character globals be looked up? #62

DavisVaughan opened this issue Jul 29, 2020 · 0 comments

Comments

@DavisVaughan
Copy link

DavisVaughan commented Jul 29, 2020

I have this issue in furrr as well, but wanted to start discussion here so we can both do the same thing.

In the following example, I understand why wrapper2() always finds y, but I would imagine that wrapper("y") should also find y. This is something that I've hit while writing tests for furrr.

I think the issue is that the environment used to find these globals is just the execution environment of future_lapply(). It is created here:

future.envir <- environment() ## Not used; just to clarify the above.

And passed through and eventually used here:

globals <- globalsByName(globals, envir = envir, mustExist = FALSE)

The parent environment of this env is the future.apply namespace environment, so there is no way for it to find y. I'm wondering if instead we should be calling envir <- parent.frame() from future_lapply() and friends to find globals. That would contain y, and I think it would have a correct chain of environments for finding other globals further up. Thoughts?

library(future.apply)
#> Loading required package: future
plan(multisession, workers = 2)

fn <- function(x) {
  exists("y")
}

wrapper <- function(future.globals = TRUE) {
  y <- 1
  future_lapply(1, fn, future.globals = future.globals)
}

wrapper2 <- function(future.globals = TRUE) {
  y <- 1
  
  # finds `y` from enclosing env of `fn`
  fn <- function(x) {
    exists("y")
  }
  
  future_lapply(1, fn, future.globals = future.globals)
}

wrapper()
#> [[1]]
#> [1] FALSE
wrapper("y")
#> [[1]]
#> [1] FALSE

wrapper2()
#> [[1]]
#> [1] TRUE
wrapper2("y")
#> [[1]]
#> [1] TRUE

Created on 2020-07-29 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants