What happens is that the function env of fn() is the global env. When future_map() searches for globals, it searches in the caller env, which contains y <- 1. So y is identified as a global and is exported to the worker and assigned into the global env on the worker, so it is found in the multisession plan
library(purrr)
library(furrr)
fn <- function(x) {
y
}
wrapper <- function() {
y <- 1
fn(1)
}
wrapper2 <- function() {
y <- 1
map(1:5, fn)
}
wrapper3 <- function() {
y <- 1
future_map(1:5, fn)
}
wrapper()
#> Error in fn(1): object 'y' not found
wrapper2()
#> Error in .f(.x[[i]], ...): object 'y' not found
plan(sequential)
wrapper3()
#> Error in ...furrr_fn(...): object 'y' not found
plan(multisession, workers = 2)
wrapper3()
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 1
#>
#> [[3]]
#> [1] 1
#>
#> [[4]]
#> [1] 1
#>
#> [[5]]
#> [1] 1
Somewhat related to https://github.com/HenrikBengtsson/future.apply/issues/62, since that also uses this env.
y
actually shouldn't be found here.What happens is that the function env of
fn()
is the global env. Whenfuture_map()
searches for globals, it searches in the caller env, which containsy <- 1
. Soy
is identified as a global and is exported to the worker and assigned into the global env on the worker, so it is found in the multisession planCreated on 2020-08-10 by the reprex package (v0.3.0)