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

前端 未结 7 1251
青春惊慌失措
青春惊慌失措 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:24

    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)
    }
    
    0 讨论(0)
提交回复
热议问题