-
Notifications
You must be signed in to change notification settings - Fork 18
Description
Both purrr and furrr's map*
functions have a .progress
switch to enable a progress bar. It would be very convenient to also have it available, e.g. in future_lapply.
Here is a quick implementation of a .progress
switch which uses a CLI-based progress bar, that is also in use in purrr and that also seems to be favoured by furrr:
library("future")
library("future.apply")
library("progressr")
f <- function(x){
Sys.sleep(.01)
x
}
future_lapply_progress <- function(X, FUN, ..., .progress = FALSE){
handlers("cli")
if(.progress == TRUE){
with_progress({
nX <- length(X)
p <- progressor(nX)
FUN_progress <- function(X, ...) {
res <- FUN(X, ...)
p()
res
}
future_lapply(X, FUN_progress, ...)
})
} else {
future_lapply(X, FUN, ...)
}
}
res <- future_lapply_progress(1:100, f, .progress = F)
res <- future_lapply_progress(1:100, f, .progress = T)
I know about futureverses "back-end/ront-end" design philisophy to separate progress signaling from displaying progress. However, if one quickly wants to parallelize some lapply-type code in RStudio, having a simple .progress switch is more convenient than having to set up the progressor, including it in the function to be parallelized, wrapping it in a with_progress()
and remembering/researching how these three pieces fit together. Also, this implementation would possibly serve as a template to update the furrr's .progress
implementation to using the CLI package.