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

1
2
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

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

...

Additional named arguments to FUN

Details

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

Value

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

Author(s)

Landon Sego

See Also

lapply, parLapply, plapply

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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)

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