Learn R Programming

surveillance (version 1.20.3)

plapply: Verbose and Parallel lapply

Description

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.

Usage

plapply(X, FUN, ...,
        .parallel = 1, .seed = NULL, .verbose = TRUE)

Value

a list of the results of calling FUN on each value of X.

Arguments

X,FUN,...

see lapply.

.parallel

the number of processes to use in parallel operation, or a "cluster" object (see makeCluster). If a number, mclapply (forking) is used on Unix-alikes, whereas on Windows parLapply is used on a newly created cluster of the specified size, which is stopped when exiting the function. By default (.parallel = 1), the basic lapply 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 running parLapply. Otherwise, set.seed(.seed) is called and the RNGkind is changed to "L'Ecuyer-CMRG" if .parallel > 1 (see the section on random numbers in the documentation of mcparallel in package parallel). If .seed is non-NULL, the original .Random.seed will be restored on.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 primitive FUNctions. The default (TRUE) will show a txtProgressBar (if .parallel = 1 in an interactive R session) or cat(".") (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 executed on.exit of FUN and may thus involve any objects from the local evaluation environment.

Author

Sebastian Meyer

See Also

Examples

Run this code
## 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)

## using a predefined cluster
library("parallel")
cl <- makeCluster(getOption("cl.cores", 2))
res <- plapply(x, quantile, probs = 1:3/4, .parallel = cl)
stopCluster(cl)

Run the code above in your browser using DataLab