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 wit
This code implements a progress bar tracking a parallelized foreach
loop using the doMC
backend, and using the excellent progress package in R
. It assumes that all cores, specified by numCores
, do an approximately equal amount of work.
library(foreach)
library(doMC)
library(progress)
iterations <- 100
numCores <- 8
registerDoMC(cores=numCores)
pbTracker <- function(pb,i,numCores) {
if (i %% numCores == 0) {
pb$tick()
}
}
pb <- progress_bar$new(
format <- " progress [:bar] :percent eta: :eta",
total <- iterations / numCores, clear = FALSE, width= 60)
output = foreach(i=1:iterations) %dopar% {
pbTracker(pb,i,numCores)
Sys.sleep(1/20)
}