clusterApply: Apply a function to a list using the cluster

View source: R/Cluster.R

clusterApplyR Documentation

Apply a function to a list using the cluster


Apply a function to a list using the cluster


clusterApply(cluster, x, fun, ..., stopOnError = FALSE, progressBar = TRUE)



The cluster of threads to run the function.


The list on which the function will be applied.


The function to apply. Note that the context in which the function is specifies matters (see details).


Additional parameters for the function.


Stop when one of the threads reports an error? If FALSE, all errors will be reported at the end.


Show a progress bar?


The function will be executed on each element of x in the threads of the cluster. If there are more elements than threads, the elements will be queued. The progress bar will show the number of elements that have been completed. It can sometimes be important to realize that the context in which a function is created is also transmitted to the worker node. If a function is defined inside another function, and that outer function is called with a large argument, that argument will be transmitted to the worker node each time the function is executed. It can therefore make sense to define the function to be called at the package level rather than inside a function, to save overhead.


A list with the result of the function on each item in x.


fun <- function(x) {
  return (x^2)

cluster <- makeCluster(numberOfThreads = 3)
clusterApply(cluster, 1:10, fun)

ParallelLogger documentation built on June 7, 2022, 5:05 p.m.