[英]Environment/scoping in furrr: nesting get() in future_map()
I'm interested in learning more about how furrr
finds stuff from the global environment, and asked generally about the black magic it performs.我有兴趣了解更多有关
furrr
如何从全球环境中查找内容的信息,并大致询问了它执行的黑魔法。 Here's a specific example of a behavior I didn't understand and could use some help with: What do I need to change in either in the future_map
call or in the get
call to return "C"
and "F"
?这是一个我不理解的行为的具体示例,可以使用一些帮助:我需要在
future_map
调用或get
调用中更改什么以返回"C"
和"F"
?
# load furrr, describe "plan"
library(furrr)
nc<-2
plan(strategy = multiprocess, workers = nc)
# create objects
a<-list("A", "B", "C")
b<-list("D", "E", "F")
#works fine
future_map(1:5, function(foo){
map(c("a", "b"), function(my_object_name){
bar<-my_object_name
print(bar)
})
})
# object 'a' not found
future_map(1:5, function(foo){
map(c("a", "b"), function(my_object_name){
bar<-get(my_object_name)[[3]]
print(bar)
})
})
EDIT编辑
It seems like this issue is not reproducible on all systems and may have to do with my installation of furrr.
这个问题似乎无法在所有系统上重现,可能与我安装的
furrr.
Despite the warning the package gives about multicore plans, this is an issue with multiprocess
and multisession
but not plan(strategy=multicore,...
.尽管软件包给出了关于多核计划的警告,但这是
multiprocess
和multisession
的问题,但不是plan(strategy=multicore,...
.
It is the envir
that is creating the problem.正是
envir
造成了问题。 Specify the envir
as the global environment to look for that object and print
将
envir
指定为全局环境以查找该对象并print
library(furrr)
future_map(1:5, function(foo){
map(c("a", "b"), function(my_object_name){
bar <- get(my_object_name, envir = .GlobalEnv)[[3]]
print(bar)
})
})
#[1] "C"
#[1] "F"
#[1] "C"
#[1] "F"
#[1] "C"
#[1] "F"
#[1] "C"
#[1] "F"
#[1] "C"
#[1] "F"
#[[1]]
#[[1]][[1]]
#[1] "C"
#[[1]][[2]]
#[1] "F"
#...
I think I tripped across some known weird behavior with the future
package with documented workarounds.我想我在带有文档化解决方法的
future
包中遇到了一些已知的奇怪行为。 See vignette in future documentation .请参阅未来文档中的小插图。
To add variables to the exported globals, use the globals
argument in future
, which translates in furrr
to , .options = future_options(globals(structure=T, add="missing_object"
要将变量添加到导出的全局变量,请在
future
使用globals
参数,它将furrr
为, .options = future_options(globals(structure=T, add="missing_object"
I suspect this might have also been one of my problems:我怀疑这可能也是我的问题之一:
...The above error occurs because, contrary to the master R process, the R worker that evaluated the
future
expression does not havedata.table
loaded....发生上述错误的原因是,与主 R 进程相反,评估
future
表达式的 R 工作人员没有加载data.table
。 Instead the evaluation falls back to the [.data.frame method, which is not what we want.相反,评估返回到 [.data.frame 方法,这不是我们想要的。
Until the future framework manages to identify
data.table
as a required package (which is the goal), we can guide future by specifying additional packages needed...在未来的框架设法将
data.table
识别为所需的包(这是目标)之前,我们可以通过指定所需的其他包来指导未来......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.