Verbose and Parallel lapply
plapply.RdVerbose and parallelized version of lapply wrapping around
mclapply and parLapply
in the base package parallel.
This wrapper can take care of the .Random.seed and
print progress information (not for cluster-based parallelization).
With the default arguments it equals lapply
enriched by a progress bar.
Arguments
- X,FUN,...
see
lapply.- .parallel
the number of processes to use in parallel operation, or a
"cluster"object (seemakeCluster). If a number,mclapply(forking) is used on Unix-alikes, whereas on WindowsparLapplyis used on a newly created cluster of the specified size, which is stopped when exiting the function. By default (.parallel = 1), the basiclapplyis used.- .seed
If set (non-
NULL), results involving random number generation become reproducible. If using a cluster (see the.parallelargument),clusterSetRNGStreamis called with the specified.seedbefore runningparLapply. Otherwise,set.seed(.seed)is called and theRNGkindis changed to"L'Ecuyer-CMRG"if.parallel > 1(see the section on random numbers in the documentation ofmcparallelin package parallel). If.seedis non-NULL, the original.Random.seedwill be restoredon.exitof the function.- .verbose
if and how progress information should be displayed, i.e., what to do on each exit of
FUN. This is unsupported and ignored for cluster-based parallelization and primitiveFUNctions. The default (TRUE) will show atxtProgressBar(if.parallel = 1in aninteractiveR session) orcat(".")(otherwise). Other choices for the dot are possible by specifying the desired symbol directly as the.verboseargument. Alternatively,.verbosemay be any custom call or expression to be executedon.exitofFUNand may thus involve any objects from the local evaluation environment.
Examples
## example inspired by help("lapply")
x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE))
## if neither parallel nor verbose then this simply equals lapply()
plapply(x, quantile, probs = 1:3/4, .verbose = FALSE)
## verbose lapply() -- not really useful for such fast computations
res <- plapply(x, quantile, probs = 1:3/4, .verbose = TRUE)
res <- plapply(x, quantile, probs = 1:3/4, .verbose = "|")
res <- plapply(x, quantile, probs = 1:3/4,
.verbose = quote(cat("length(x) =", length(x), "\n")))
## setting the seed for reproducibility of results involving the RNG
samp <- plapply(as.list(1:3), runif, .seed = 1)
## parallel lapply()
res <- plapply(x, quantile, probs = 1:3/4, .parallel = 2, .verbose = FALSE)
## using a predefined cluster
library("parallel")
cl <- makeCluster(getOption("cl.cores", 2))
res <- plapply(x, quantile, probs = 1:3/4, .parallel = cl)
stopCluster(cl)