Progress updates for 'foreach' functions
Henrik Bengtsson
Source:vignettes/progressify-41-foreach.md
progressify-41-foreach.RmdThe progressify package allows you to easily add
progress reporting to sequential and parallel map-reduce code by piping
to the progressify() function. Easy!
TL;DR
library(progressify)
handlers(global = TRUE)
library(foreach)
slow_fcn <- function(x) {
Sys.sleep(0.1) # emulate work
x^2
}
xs <- 1:100
ys <- foreach(x = xs) %do% slow_fcn(x) |> progressify()Introduction
This vignette demonstrates how to use this approach to add progress
reporting to the foreach
foreach() construct and the doFuture
%dofuture% operator.
For example, consider:
This foreach() construct provides no feedback on how far
it has progressed. We can easily add progress reporting by piping to
progressify():
library(foreach)
library(progressify)
handlers(global = TRUE)
xs <- 1:100
ys <- foreach(x = xs) %do% slow_fcn(x) |> progressify()Using the default progress handler, the progress reporting will appear as:
|===== | 20%
With doFuture
The same approach works with the doFuture package for parallel foreach evaluation:
library(doFuture)
plan(multisession)
library(progressify)
handlers(global = TRUE)
xs <- 1:100
ys <- foreach(x = xs) %dofuture% slow_fcn(x) |> progressify()Supported Functions
The progressify() function supports the following
foreach operators:
foreach(...) %do% { ... }foreach(...) %dopar% { ... }
and the following doFuture operator:
foreach(...) %dofuture% { ... }