Tracking mclapply with progress bar

Share:

Description

pbmclapply is a wrapper around the mclapply function. It adds a progress bar to mclapply function.

This function works on *nix (Linux, Unix such as macOS) system only due to the lack of fork() functionality, which is essential for mcapply, on Windows.

Usage

1
pbmclapply(X, FUN, ..., mc.style = 3, mc.cores =getOption("mc.cores", 2L))

Arguments

X

a vector (atomic or list) or an expressions vector. Otherobjects (including classed objects) will be coerced by 'as.list'.

FUN

the function to be applied to.

...

optional arguments to FUN.

mc.cores

see mclapply.

mc.style

style of the progress bar. See txtProgressBar.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# A lazy sqrt function which doesn't care about efficiency
lazySqrt <- function(num) {
  # Sleep randomly between 0 to 1 second
  Sys.sleep(runif(1))
  return(sqrt(num))
}
# A lazy and chatty sqrt function.
# An example of passing arguments to pbmclapply.
lazyChattySqrt <- function(num, name) {
  # Sleep randomly between 0 to 1 second
  Sys.sleep(runif(1))
  return(sprintf("Hello %s, the sqrt of %f is %f.", toString(name), num, sqrt(num)))
}
# Get the sqrt of 1-5 in parallel
if (length(grep("windows", Sys.info()["sysname"], ignore.case = TRUE))) {
  # Windows Machine does not support mc(*)apply
  # result <- pbmclapply(1:3, lazySqrt, mc.cores = 1)
  # chattyResult <- pbmclappy(1:3, lazyChattySqrt, "Bob", mc.cores = 1)
} else {
  # Get the sqrt of 1-5 in parallel
  # result <- pbmclapply(1:5, lazySqrt)
  # chattyResult <- pbmclapply(1:3, lazyChattySqrt, "Bob")
}

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.