How do you create a progress bar when using the "foreach()" function in R? -
there informative posts on how create counter loops in r program. however, how create similar function when using parallelized version "foreach()"?
edit: after update dosnow package has become quite simple display nice progress bar when using %dopar%
, works on linux, windows , os x
dosnow
officially supports progress bars via .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 way of tracking progress, if keep in mind total number of iterations, set .verbose = t
print console iterations have been finished.
previous solution linux , os x
on ubuntu 14.04 (64 bit) , os x (el capitan) progress bar displayed when using %dopar%
if in makecluster
function oufile = ""
set. not seem work under windows. on makecluster
:
outfile: direct stdout , stderr connection output workers. "" indicates no redirection (which may useful workers on local machine). defaults ‘/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 progress bar looks like. looks little odd since new bar printed every progression of bar , because worker may lag bit causes progress bar go , forth occasionally.
Comments
Post a Comment