Title: | Tracking the Progress of Mc*pply with Progress Bar |
---|---|
Description: | A light-weight package helps you track and visualize the progress of parallel version of vectorized R functions (mc*apply). Parallelization (mc.core > 1) works only on *nix (Linux, Unix such as macOS) system due to the lack of fork() functionality, which is essential for mc*apply, on Windows. |
Authors: | Kevin Kuang (aut), Quyu Kong (ctb), Francesco Napolitano (ctb) |
Maintainer: | Kevin kuang <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.5.1 |
Built: | 2024-10-27 05:49:26 UTC |
Source: | https://github.com/kvnkuang/pbmcapply |
pbmclapply
is a wrapper around the mclapply
function. It adds a progress bar to mclapply
function.
Parallelization (mc.core > 1) works only on *nix (Linux, Unix such as macOS) system due to the lack of fork()
functionality, which is essential for mcapply, on Windows.
pbmclapply(X, FUN, ..., mc.style = "ETA", mc.substyle = NA, mc.cores = getOption("mc.cores", 2L), ignore.interactive = getOption("ignore.interactive", F), mc.preschedule = TRUE, mc.set.seed = TRUE, mc.cleanup = TRUE, mc.allow.recursive = TRUE)
pbmclapply(X, FUN, ..., mc.style = "ETA", mc.substyle = NA, mc.cores = getOption("mc.cores", 2L), ignore.interactive = getOption("ignore.interactive", F), mc.preschedule = TRUE, mc.set.seed = TRUE, mc.cleanup = TRUE, mc.allow.recursive = TRUE)
X |
a vector (atomic or list) or an expressions vector. Other objects (including classed objects) will be coerced by |
FUN |
the function to be applied to. |
... |
optional arguments to FUN. |
mc.cores |
see |
mc.style , mc.substyle
|
style of the progress bar. See |
ignore.interactive |
whether the |
mc.preschedule , mc.set.seed , mc.cleanup , mc.allow.recursive
|
See |
# A lazy sqrt function which doesn't care about efficiency lazySqrt <- function(num) { # Sleep randomly between 0 to 0.5 second Sys.sleep(runif(1, 0, 0.5)) return(sqrt(num)) } # On Windows, set cores to be 1 if (.Platform$OS.type == "windows") { cores = 1 } else { cores = 2 } # A lazy and chatty sqrt function. # An example of passing arguments to pbmclapply. lazyChattySqrt <- function(num, name) { # Sleep randomly between 0 to 0.5 second Sys.sleep(runif(1, 0, 0.5)) return(sprintf("Hello %s, the sqrt of %f is %f.", toString(name), num, sqrt(num))) } # Get the sqrt of 1-3 in parallel result <- pbmclapply(1:3, lazySqrt, mc.cores = cores) chattyResult <- pbmclapply(1:3, lazyChattySqrt, "Bob", mc.cores = cores)
# A lazy sqrt function which doesn't care about efficiency lazySqrt <- function(num) { # Sleep randomly between 0 to 0.5 second Sys.sleep(runif(1, 0, 0.5)) return(sqrt(num)) } # On Windows, set cores to be 1 if (.Platform$OS.type == "windows") { cores = 1 } else { cores = 2 } # A lazy and chatty sqrt function. # An example of passing arguments to pbmclapply. lazyChattySqrt <- function(num, name) { # Sleep randomly between 0 to 0.5 second Sys.sleep(runif(1, 0, 0.5)) return(sprintf("Hello %s, the sqrt of %f is %f.", toString(name), num, sqrt(num))) } # Get the sqrt of 1-3 in parallel result <- pbmclapply(1:3, lazySqrt, mc.cores = cores) chattyResult <- pbmclapply(1:3, lazyChattySqrt, "Bob", mc.cores = cores)
pbmcmapply
is a wrapper around the mcmapply
function. It adds a progress bar to mcmapply
function.
Parallelization (mc.core > 1) works only on *nix (Linux, Unix such as macOS) system due to the lack of fork()
functionality, which is essential for mcapply, on Windows.
pbmcmapply(FUN, ..., MoreArgs = NULL, mc.style = "ETA", mc.substyle = NA, mc.cores = getOption("mc.cores", 2L), ignore.interactive = getOption("ignore.interactive", F), mc.preschedule = TRUE, mc.set.seed = TRUE, mc.cleanup = TRUE)
pbmcmapply(FUN, ..., MoreArgs = NULL, mc.style = "ETA", mc.substyle = NA, mc.cores = getOption("mc.cores", 2L), ignore.interactive = getOption("ignore.interactive", F), mc.preschedule = TRUE, mc.set.seed = TRUE, mc.cleanup = TRUE)
FUN |
the function to be applied in parallel to ... |
... |
arguments to vectorize over (vectors or lists of strictly positive length, or all of zero length). |
MoreArgs |
a list of other arguments to FUN. |
mc.cores |
see |
mc.style , mc.substyle
|
style of the progress bar. See |
ignore.interactive |
whether the |
mc.preschedule , mc.set.seed , mc.cleanup
|
See |
# A lazy sqrt function which doesn't care about efficiency lazySqrt <- function(num) { # Sleep randomly between 0 to 0.5 second Sys.sleep(runif(1, 0, 0.5)) return(sqrt(num)) } # On Windows, set cores to be 1 if (.Platform$OS.type == "windows") { cores = 1 } else { cores = 2 } # A lazy and chatty sqrt function. # An example of passing arguments to pbmcmapply. lazyChattySqrt <- function(num, name) { # Sleep randomly between 0 to 0.5 second Sys.sleep(runif(1, 0, 0.5)) return(sprintf("Hello %s, the sqrt of %f is %f.", toString(name), num, sqrt(num))) } # Get the sqrt of 1-3 in parallel result <- pbmcmapply(lazySqrt, 1:3, mc.cores = cores) chattyResult <- pbmcmapply(lazyChattySqrt, 1:3, MoreArgs = list("Bob"), mc.cores = cores)
# A lazy sqrt function which doesn't care about efficiency lazySqrt <- function(num) { # Sleep randomly between 0 to 0.5 second Sys.sleep(runif(1, 0, 0.5)) return(sqrt(num)) } # On Windows, set cores to be 1 if (.Platform$OS.type == "windows") { cores = 1 } else { cores = 2 } # A lazy and chatty sqrt function. # An example of passing arguments to pbmcmapply. lazyChattySqrt <- function(num, name) { # Sleep randomly between 0 to 0.5 second Sys.sleep(runif(1, 0, 0.5)) return(sprintf("Hello %s, the sqrt of %f is %f.", toString(name), num, sqrt(num))) } # Get the sqrt of 1-3 in parallel result <- pbmcmapply(lazySqrt, 1:3, mc.cores = cores) chattyResult <- pbmcmapply(lazyChattySqrt, 1:3, MoreArgs = list("Bob"), mc.cores = cores)
This is an extended version of the txtProgressBar
function with the estimated time to completion (ETA). Please refer to that for documentation (help(utils::txtProgressBar)
).
The original utils::setTxtProgressBar
can be used to update the bar.
Use help(setTxtProgressBar, "utils")
to get help about the original function.
progressBar(min = 0, max = 1, initial = 0, style = "ETA", substyle = NA, char = "=", width = NA, file = "")
progressBar(min = 0, max = 1, initial = 0, style = "ETA", substyle = NA, char = "=", width = NA, file = "")
min , max , initial
|
see |
style |
style of the progress bar - see 'Details'. |
substyle |
substyle of the progress bar - only needed when style is set to certain value (see 'Details'). |
char , width , file
|
see |
When style = "txt", it performs exactly the same as the original txtProgressBar
. In this case, substyle shall be treated as the style in the original txtProgressBar
. Please refer to the 'Detail' of txtProgressBar
for the meanings of substyles.
When style = "ETA", it shows a progress bar with the estimated time to completion (ETA). Substyle is not used in this case. However, when running in a terminal and the width of the terminal windows is smaller than 40 characters, the progress bar will not be displayed.
An object of class "txtProgressBar".
Code derived from library pbarETA
(https://github.com/franapoli/pbarETA) by Francesco Napolitano [email protected].
# Test function testit <- function(x, ...) { pb <- progressBar(...) for(i in c(0, x, 1)) { setTxtProgressBar(pb, i) } close(pb) } # Txt progress bar testit(sort(runif(10)), style = "txt", substyle = 3) # ETA progress bar testit(sort(runif(10)), style = "ETA")
# Test function testit <- function(x, ...) { pb <- progressBar(...) for(i in c(0, x, 1)) { setTxtProgressBar(pb, i) } close(pb) } # Txt progress bar testit(sort(runif(10)), style = "txt", substyle = 3) # ETA progress bar testit(sort(runif(10)), style = "ETA")