I’ve tried a few different approaches for parallel processing in R but the function I’ve found easiest to use is foreach. The only trick is that you need to register a “parallel backend” – doMC works well for unix systems (including OS X).
Specifying the .combine argument allows you to customise how the results are aggregated at the end of the loop.
Simple example:
require(doMC)
require(foreach)
registerDoMC(cores=3)
n=10
result = foreach(j = 1:n, .combine=rbind) %dopar% {
# EXPERIMENT
# last line is returned as a row in the result matrix
rep(j,4)
}
The output:
result
[,1] [,2] [,3] [,4]
result.1 1 1 1 1
result.2 2 2 2 2
result.3 3 3 3 3
result.4 4 4 4 4
result.5 5 5 5 5
result.6 6 6 6 6
result.7 7 7 7 7
result.8 8 8 8 8
result.9 9 9 9 9
result.10 10 10 10 10