Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating progress bars from each parallel worker using foreach and doParallel
library(foreach)
library(iterators)
library(doParallel)
library(tcltk)
# Choose number of iterations
n <- 1000
cl <- makeCluster(8)
registerDoParallel(cl)
time3 <- system.time({
clusterExport(cl, c("n")) # Export max number of iteration to workers
k <- foreach(i = icount(n), .packages = "tcltk", .combine = c) %dopar% {
if(!exists("pb")) pb <- tkProgressBar("Parallel task", min=1, max=n)
setTkProgressBar(pb, i)
Sys.sleep(0.05)
log2(i)
}
})
#Stop the cluster
stopCluster(cl)
print(time3)
@bshor

This comment has been minimized.

Copy link

@bshor bshor commented May 6, 2015

I tried this code but got the error, " Error in { : task 1 failed - "non-numeric argument to mathematical function"

@MySchizoBuddy

This comment has been minimized.

Copy link

@MySchizoBuddy MySchizoBuddy commented Jun 6, 2015

works fine for me

@yaakovfeldman

This comment has been minimized.

Copy link

@yaakovfeldman yaakovfeldman commented Mar 24, 2017

Don't think this works with windows.

@thaoz

This comment has been minimized.

Copy link

@thaoz thaoz commented Jan 10, 2018

This works fine for me on windows .

@thiagoveloso

This comment has been minimized.

Copy link

@thiagoveloso thiagoveloso commented Jul 15, 2019

Any way to do this under a nested foreach loop?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.