Globals bug using future_map
Closed this issue · 1 comments
abdellah19jan commented
Hi,
I didn't receive a response to my last post in HenrikBengtsson/globals#77 since 28 days, if you could give me a solution to work around the problem (see code below), it will break my deadlock!!!
future_lapply
didn't solve the problem.
Thanks in advance!
library(future.apply)
library(furrr)
library(tidyverse)
plan(multisession, workers = 2)
fun <- function(arg) arg + 1
dic <- c("mod" = "fun")
#=================================================#
# REFERRING TO THE FUNCTION AS A CHARACTER STRING #
#=================================================#
f <- function(x) invoke_map(dic[[x]], 2)
map("mod", f)
#> [[1]]
#> [[1]][[1]]
#> [1] 3
future_map("mod", f)
#> Error in fun(2) : could not find function "fun"
future_lapply("mod", f)
#> Error in fun(2) : could not find function "fun"
#===========================================#
# PASSING THE ACTUAL FUNCTION TO invoke_map #
#===========================================#
f <- function(x) invoke_map(eval(parse(text = dic[[x]])), 2)
map("mod", f)
#> [[1]]
#> [[1]][[1]]
#> [1] 3
future_map("mod", f)
#> Error in eval(parse(text = dic[[x]])) : object 'fun' not found
future_lapply("mod", f)
#> Error in eval(parse(text = dic[[x]])) : object 'fun' not found
DavisVaughan commented
Make the values of your dic
dictionary the actual function object, rather than just a character string of their name. That ensures that globals can find them.
library(furrr)
#> Loading required package: future
library(purrr)
fun <- function(arg) arg + 1
dic <- c("mod" = fun) # <- CHANGE IS HERE
f <- function(x) invoke_map(dic[[x]], 2)
map("mod", f)
#> [[1]]
#> [[1]][[1]]
#> [1] 3
plan(multisession, workers = 2)
future_map("mod", f)
#> [[1]]
#> [[1]][[1]]
#> [1] 3
plan(sequential)
Created on 2021-11-24 by the reprex package (v2.0.1)