cl_lapply() is a wrapper for pbapply::pblapply() that handles cluster checking, set up, batch processing and cluster closure.
Usage
cl_lapply(
.x,
.fun,
...,
.cl = NULL,
.varlist = NULL,
.envir = .GlobalEnv,
.chunk = FALSE,
.chunk_fun = NULL,
.use_names = TRUE,
.combine = NULL
)
cl_chunk(.cl)Arguments
- .x
A
listor sequence of objects over which to iterate.- .fun
A
functionthat is applied to elements of.x. This must accept:Elements of
.xas the first argument;A
.chunkargsargument if.chunk_fun(below) is supplied;
- ...
Additional arguments passed to
.funand.chunk_fun(if supplied). Since...is passed to both functions,.funand.check_funmust be able to handle unused arguments.- .cl, .varlist, .envir
(optional) Cluster arguments.
.clis the cluster argument passed topbapply::pblapply(), supplied as:A
clusterobject fromparallel::makeCluster()or a sister function;An
integerthat defines the number of child processes;
.varlistis acharactervector of objects for export (seeparallel::clusterExport())..enviris theenvironmentfrom which to export variables (seeparallel::clusterExport()).
- .chunk, .chunk_fun
(optional) Chunk arguments.
chunkis alogicalvector that defines whether to parallelise over.xor batches of.x(chunks).If
.chunk = FALSE, function behaviour matchespbapply::pblapply().If
.chunk = TRUE,.xis split into a series of chunks that are processed in parallel; within each chunk,.funis applied to each.xelement. This reduces the parallelisation overhead.
.chunk_funis afunctionimplemented once for each chunk (unlike.funwhich is implemented for every element of every chunk). This must accept the following argument(s):Elements of
.xfor a specific chunk as the first argument;
The output of
.chunk_funis made available to.funvia.chunkargs.- .use_names
A
logicalvariable that defines whether or use.x's names to name output elements. This is silently ignored if.combineis specified.- .combine
(optional) A
functionthat defines how to combinelistelements. If.combine = NULL, alistis returned. Other suitable options are, for example,purrr::list_flatten()anddata.table::rbindlist().
Value
cl_lapply()invisibly returns alistor a combinedlist(defined by.combine).cl_chunk()returns alogicalvariable.
Details
cl_lapply() is exported, as we have found it useful in other projects, but primarily intended for internal use. Use pbapply::pboptions() to control the progress bar, including the number of gradations (nout). nout also controls the number of chunks on each core. Fewer chunks reduce parallelisation overhead but also the number of gradations on the progress bar.
cl_chunk() sets the default chunk behaviour of cl_lapply() in wrapper functions:
If a single core is specified,
cl_chunk()returnsFALSE.Otherwise,
cl_chunk()returnsTRUE.
cl_lapply() and associated (internal) functions evolved from flapper::cl_*() functions.
See also
See flapper-tips-parallel for further information about parallelisation, including the differences between socket clusters and forking.
