Learn R Programming

Smisc (version 0.3.9.1)

parLapplyW: A wrapper for parLapply

Description

A wrapper to make calls to parLapply easier by initializing the cluster, exporting objects and expressions to the worker nodes, and shutting down the cluster.

Usage

parLapplyW(X, FUN, ..., njobs = parallel::detectCores() - 1, expr = NULL,
  varlist = NULL, envir = parent.frame())

Arguments

X

A vector (atomic or list)

FUN

A function or character string naming a function whose first argument will be passed the elements of X

Additional named arguments to FUN

njobs

The number of jobs (cores) to use

expr

An expression that will be evaluated on each worker node via a call to clusterEvalQ

varlist

Character vector of names of objects to export to each worker node via clusterExport

envir

The environment containing the variables in varlist that will be exported

Value

The same result given by lapply(X, FUN, ...)

Details

The expression in expr is evaluated before the variables in varlist are exported.

See Also

lapply, parLapply, plapply

Examples

Run this code
# NOT RUN {
# Create a simple list
a <- list(a = rnorm(10), b = rnorm(20), c = rnorm(15))

# Some objects that will be needed by f1:
b1 <- rexp(20)
b2 <- rpois(10, 20)

# The function, which will depend on the Smisc package
f1 <- function(x, someText = "this.stuff") {
 textJunk <- stripExtension(someText)
 result <- mean(x) + max(b1) - min(b2)
 return(list(textJunk, result))
}

# Call parLapplyW(), loading the Smisc package and passing in the "b1" and "b2" objects
res.1 <- parLapplyW(a, f1, someText = "that.stuff", njobs = 2,
                   expr = expression(library(Smisc)),
                   varlist = c("b1", "b2"))

print(res.1)

# Call parLapplyW(), note that we're sending a different value for "b2" into the worker nodes
# via the 'expr' argument
res.2 <- parLapplyW(a, f1, someText = "that.stuff", njobs = 2,
                   expr = expression({library(Smisc); b2 <- rnorm(10)}),
                   varlist = c("b1"))

# These should not be equivalent
identical(res.1, res.2)

# Call lapply
res.3 <- lapply(a, f1, someText = "that.stuff")

# Compare results, these should be equivalent
identical(res.1, res.3)

# }

Run the code above in your browser using DataLab