Verbose and Parallel lapply
plapply.Rd
Verbose 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 WindowsparLapply
is used on a newly created cluster of the specified size, which is stopped when exiting the function. By default (.parallel = 1
), the basiclapply
is used.- .seed
If set (non-
NULL
), results involving random number generation become reproducible. If using a cluster (see the.parallel
argument),clusterSetRNGStream
is called with the specified.seed
before runningparLapply
. Otherwise,set.seed(.seed)
is called and theRNGkind
is changed to"L'Ecuyer-CMRG"
if.parallel > 1
(see the section on random numbers in the documentation ofmcparallel
in package parallel). If.seed
is non-NULL
, the original.Random.seed
will be restoredon.exit
of 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 primitiveFUN
ctions. The default (TRUE
) will show atxtProgressBar
(if.parallel = 1
in aninteractive
R session) orcat(".")
(otherwise). Other choices for the dot are possible by specifying the desired symbol directly as the.verbose
argument. Alternatively,.verbose
may be any custom call or expression to be executedon.exit
ofFUN
and 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)