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 with "foreach()"?
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
The following code will produce a nice progress bar in R for the foreach control structure. It will also work with graphical progress bars by replacing txtProgressBar with the desired progress bar object.
While the code above answers your question in its most basic form a better and much harder question to answer is whether you can create an R progress bar which monitors the progress of a foreach statement when it is parallelized with %dopar%. Unfortunately I don't think it is possible to monitor the progress of a parallelized foreach in this way, but I would love for someone to prove me wrong, as it would be very useful feature.
You can also get this to work with the
progress
package.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.Strangely, it only displays correctly with
style = 3
.This code is a modified version of the doRedis example, and will make a progress bar even when using
%dopar%
with a parallel backend:You have to know the number of iterations and the combination function ahead of time.
You save the start time with
Sys.time()
before the loop. Loop over rows or columns or something which you know the total of. Then, inside the loop you can calculate the time ran so far (seedifftime
), percentage complete, speed and estimated time left. Each process can print those progress lines with themessage
function. You'll get an output something likeObviously the looping order will greatly affect how well this works. Don't know about
foreach
but withmulticore
'smclapply
you'd get good results usingmc.preschedule=FALSE
, which means that items are allocated to processes one-by-one in order as previous items complete.This code implements a progress bar tracking a parallelized
foreach
loop using thedoMC
backend, and using the excellent progress package inR
. It assumes that all cores, specified bynumCores
, do an approximately equal amount of work.