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

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

    This code is a modified version of the doRedis example, and will make a progress bar even when using %dopar% with a parallel backend:

    #Load Libraries
    library(foreach)
    library(utils)
    library(iterators)
    library(doParallel)
    library(snow)
    
    #Choose number of iterations
    n <- 1000
    
    #Progress combine function
    f <- function(){
      pb <- txtProgressBar(min=1, max=n-1,style=3)
      count <- 0
      function(...) {
        count <<- count + length(list(...)) - 1
        setTxtProgressBar(pb,count)
        Sys.sleep(0.01)
        flush.console()
        c(...)
      }
    }
    
    #Start a cluster
    cl <- makeCluster(4, type='SOCK')
    registerDoParallel(cl)
    
    # Run the loop in parallel
    k <- foreach(i = icount(n), .final=sum, .combine=f()) %dopar% {
      log2(i)
    }
    
    head(k)
    
    #Stop the cluster
    stopCluster(cl)
    

    You have to know the number of iterations and the combination function ahead of time.

提交回复
热议问题