r - 使用 doParallel + foreach 显示 progress_bar
问题描述
我正在使用此处发布的示例代码来显示带有+的progress_bar
(来自progress
包)。然而,那里的解决方案使用(例如我用于测试的 Dewey Brooke 的代码),它比构建带有 CRAN 标志的包时更过时并返回此注释:doParallel
foreach
doSNOW
doParallel
使用被取代的包:'doSNOW (>= 1.0.19)'</p>
改变这似乎并不像预期的那么简单。If onlyregisterDoSNOW
替换为,registerDoParallel
并且代码将运行,但在第二种情况下根本不会显示任何进度条。.options.snow
.options.doparallel
我认为这可能与使用.options.X
. 这部分代码对我来说非常模糊,因为.options.snow
在 using 时确实有效doSNOW
,但是没有foreach
关于使用此参数的手册页的文档。因此,不起作用也就不足为奇了.options.doparallel
,因为这只是我的一个疯狂猜测。
pb$tick()
在循环中包含对的调用foreach
也不起作用,实际上会导致结果错误。所以我真的不知道应该在代码中的什么位置包含它。
从哪里来.options.snow
?应该去哪里pb$tick()
,如何显示这里progress_bar
使用的对象doParallel
?
为方便起见,我粘贴在代码下方(doSNOW
替换为doParallel
),但再次注明原始来源:
library(parallel)
library(doParallel)
numCores<-detectCores()
cl <- makeCluster(numCores)
registerDoParallel(cl)
# progress bar ------------------------------------------------------------
library(progress)
iterations <- 100 # used for the foreach loop
pb <- progress_bar$new(
format = "letter = :letter [:bar] :elapsed | eta: :eta",
total = iterations, # 100
width = 60)
progress_letter <- rep(LETTERS[1:10], 10) # token reported in progress bar
# allowing progress bar to be used in foreach -----------------------------
progress <- function(n){
pb$tick(tokens = list(letter = progress_letter[n]))
}
opts <- list(progress = progress)
# foreach loop ------------------------------------------------------------
library(foreach)
foreach(i = 1:iterations, .combine = rbind, .options.doparallel = opts) %dopar% {
summary(rnorm(1e6))[3]
}
stopCluster(cl)
解决方案
doParallel
still uses the .options.snow
argument for whatever reason. Found this little tidbit in the doParallel documentation.
The doParallel backend supports both multicore and snow options passed through the foreach function. The supported multicore options are 1st
preschedule
,set.seed
,silent
, andcores
, which are analogous to the similarly named arguments tomclapply
, and are passed using the.options.multicore
argument to foreach. The supported snow options arepreschedule
, which like its multicore analog can be used to chunk the tasks so that each worker gets a prescheduled chunk of tasks, andattachExportEnv
, which can be used to attach the export environment in certain cases where R’s lexical scoping is unable to find a needed export. The snow options are passed to foreach using the.options.snow
argument.
foreach
is powerful package but whoever is maintaining it makes odd decisions.
推荐阅读
- firebase - 在颤动中发送电话身份验证
- python - keras 神经网络中的这个错误是什么意思?
- c - 如何在 C 中进行正确的二进制搜索
- nebular - 使用 getJsTheme() 无法访问自定义主题变量
- python - 如何查看一个图像是否包含在另一个图像图像中,如果有则输出 1,如果没有则输出 0?
- node.js - 对消息做出反应的问题
- vue.js - heroku 中的 vue-cli-service 构建失败
- scala - 如何在 IntelliJ 中更改捆绑 sbt 的 coursier 缓存目录
- javascript - 在高图地图中为动态变量格式化工具提示,一些用逗号,另一些用百分比
- reactjs - 如何在 React 中组织/管理上下文状态和辅助函数(主要是 API 调用)