使用“foreach()"时如何创建进度条?R中的函数? [英] How do you create a progress bar when using the "foreach()" function in R?

查看:35
本文介绍了使用“foreach()"时如何创建进度条?R中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些关于如何在 R 程序中为循环创建计数器的信息性帖子.但是,在使用带有foreach()"的并行版本时,如何创建类似的函数?

解决方案

Edit: After a 更新 到 doSNOW 包 使用 %dopar% 时显示漂亮的进度条变得非常简单,并且它适用于 Linux,Windows 和 OS X

doSNOW 现在通过 .options.snow 参数正式支持进度条.

库(doSNOW)cl <- makeCluster(2)注册DoSNOW(cl)迭代 <- 100pb <- txtProgressBar(max = 迭代次数,样式 = 3)进度 <- 函数(n)setTxtProgressBar(pb,n)选择 <- 列表(进度 = 进度)结果 <- foreach(i = 1:iterations, .combine = rbind,.options.snow = opts) %dopar%{s <- 总结(rnorm(1e6))[3]退货}关闭(铅)停止集群(cl)

另一种跟踪进度的方法,如果你记住迭代的总数,是设置 .verbose = T 因为这将打印到控制台,迭代已经完成.

以前的 Linux 和 OS X 解决方案

在 Ubuntu 14.04(64 位)和 OS X (El Capitan) 上,如果在 makeCluster 函数 中使用 %dopar%,进度条也会显示oufile = "" 已设置.它似乎在 Windows 下不起作用.来自 makeCluster 的帮助:

outfile:从何处引导工作人员的 stdout 和 stderr 连接输出."" 表示没有重定向(这可能只对本地机器上的工作人员有用).默认为/dev/null"(Windows 上为nul:").

示例代码:

库(foreach)图书馆(doSNOW)cl <- makeCluster(4, outfile="") # 内核数.注意'输出文件'注册DoSNOW(cl)迭代 <- 100pb <- txtProgressBar(min = 1, max = 迭代次数,style = 3)结果 <- foreach(i = 1:iterations, .combine = rbind) %dopar%{s <- 总结(rnorm(1e6))[3]setTxtProgressBar(pb, i)退货}关闭(铅)停止集群(cl)

这是进度条的样子.它看起来有点奇怪,因为每个进度条都会打印一个新的进度条,并且工作人员可能会滞后一点,这会导致进度条偶尔来回移动.

there are some informative posts on how to create a counter for loops in an R program. However, how do you create a similar function when using the parallelized version with "foreach()"?

解决方案

Edit: After an update to the doSNOW package it has become quite simple to display a nice progress bar when using %dopar% and it works on Linux, Windows and OS X

doSNOW now officially supports progress bars via the .options.snow argument.

library(doSNOW)
cl <- makeCluster(2)
registerDoSNOW(cl)
iterations <- 100
pb <- txtProgressBar(max = iterations, style = 3)
progress <- function(n) setTxtProgressBar(pb, n)
opts <- list(progress = progress)
result <- foreach(i = 1:iterations, .combine = rbind, 
                  .options.snow = opts) %dopar%
{
    s <- summary(rnorm(1e6))[3]
    return(s)
}
close(pb)
stopCluster(cl) 

Yet another way of tracking progress, if you keep in mind the total number of iterations, is to set .verbose = T as this will print to the console which iterations have been finished.

Previous solution for Linux and OS X

On Ubuntu 14.04 (64 bit) and OS X (El Capitan) the progress bar is displayed even when using %dopar% if in the makeCluster function oufile = "" is set. It does not seem to work under Windows. From the help on makeCluster:

outfile: Where to direct the stdout and stderr connection output from the workers. "" indicates no redirection (which may only be useful for workers on the local machine). Defaults to ‘/dev/null’ (‘nul:’ on Windows).

Example code:

library(foreach)
library(doSNOW)
cl <- makeCluster(4, outfile="") # number of cores. Notice 'outfile'
registerDoSNOW(cl)
iterations <- 100
pb <- txtProgressBar(min = 1, max = iterations, style = 3)
result <- foreach(i = 1:iterations, .combine = rbind) %dopar% 
{
      s <- summary(rnorm(1e6))[3]
      setTxtProgressBar(pb, i) 
      return(s)
}
close(pb)
stopCluster(cl) 

This is what the progress bar looks like. It looks a little odd since a new bar is printed for every progression of the bar and because a worker may lag a bit which causes the progress bar to go back and forth occasionally.

这篇关于使用“foreach()"时如何创建进度条?R中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆