Created
June 24, 2024 16:06
-
-
Save jmbarbone/3fb14255eb7a26809ddedf14219768c8 to your computer and use it in GitHub Desktop.
examples of using the {progressr} package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(progressr) | |
library(furrr) | |
handlers("void") | |
handlers(list( | |
handler_progress( | |
format = ":spin :current/:total (:message) [:bar] :percent in :elapsed ETA: :eta", | |
width = getOption("width"), | |
complete = "=" | |
) | |
)) | |
foo <- function(x, p = invisible) { | |
p <- progressor(along = x) | |
future_walk( | |
x, | |
function(.x, p) { | |
on.exit(p()) | |
p() | |
message(.x) | |
Sys.sleep(.x) | |
}, | |
p = p, | |
.progress = TRUE | |
) | |
} | |
plan(multisession, workers = 2) | |
with_progress(foo(runif(20)), enable = TRUE) | |
foo <- function(x, p = invisible) { | |
for (i in x) { | |
p() | |
message("i: ", i) | |
Sys.sleep(i) | |
} | |
invisible() | |
} | |
with_progress(enable = TRUE, { | |
p <- progressor(10) | |
foo(runif(10), p = p) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment