How do you create a progress bar when using the “foreach()” function in R?

前端 未结 7 1284
青春惊慌失措
青春惊慌失措 2020-11-28 03:41

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

7条回答
  •  悲&欢浪女
    2020-11-28 04:04

    This is now possible with the parallel package. Tested with R 3.2.3 on OSX 10.11, running inside RStudio, using a "PSOCK"-type cluster.

    library(doParallel)
    
    # default cluster type on my machine is "PSOCK", YMMV with other types
    cl <- parallel::makeCluster(4, outfile = "")
    registerDoParallel(cl)
    
    n <- 10000
    pb <- txtProgressBar(0, n, style = 2)
    
    invisible(foreach(i = icount(n)) %dopar% {
        setTxtProgressBar(pb, i)
    })
    
    stopCluster(cl)
    

    Strangely, it only displays correctly with style = 3.

提交回复
热议问题