简体   繁体   English

furrr 中的环境/范围:在 future_map() 中嵌套 get()

[英]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,... .尽管软件包给出了关于多核计划的警告,但这是multiprocessmultisession的问题,但不是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 printenvir指定为全局环境以查找该对象并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 have data.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.

相关问题 嵌套的 furrr::future_map? - Nested furrr::future_map? 完成后停止 furrr::future_map 打印迭代 - Stop furrr::future_map from printing iteration AFTER finishing 使用省略号 (...) 将参数传递给 furrr::future_map - Passing arguments to furrr::future_map using ellipsis (…) purrr 的地图有效,但 furrr 的未来地图无效 - purrr's map works, but furrr's future map does not future_map 中的错误:缺少参数“.f”,没有默认值 - Error in future_map: argument ".f" is missing, with no default 在另一个函数中运行 parLapply 和 future_map 不必要地将大对象复制到每个工作人员 - Running parLapply and future_map inside another function unnecessarily copies large objects to each worker 在 Golem Shiny 应用程序中使用 {future} 和 {furrr} 函数时出现错误,它来自什么? - I get an error when using {future} and {furrr} functions within a Golem Shiny App, what does it come from? 当我使用 `dplyr::mutate()` 时,为什么 `furrr::future_map_int()` 比 `purrr::map_int()` 慢? - Why is `furrr::future_map_int()` slower than `purrr::map_int()` when I use `dplyr::mutate()`? R furrr:在运行计算之前验证每个未来进程的 API - R furrr: authenticate an API on each future process before running the computation clusterExport,环境和可变范围 - clusterExport, environment and variable scoping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM