In the help page of Multicore::parallel
an example similar to this is given:
p <- parallel(1:10) q <- parallel(1:20) my.results <- collect(list(p, q)) ## wait for jobs to finish and collect all results
This in turn can be written in the following form, omitting p
and q
.
my.results <- collect(list(parallel(1:10), parallel(1:20))) ## wait for jobs to finish and collect all results
The idiom collect(list(parallel()))
is fine for ad hoc type of jobs, but often mclapply()
is the better choice. In real applications, you often have a list of tasks that needs to be done. That list can consist of elements that are the outcome of another function, or be a list of tasks created manually. Below is an example of the former. This one invokes the function my.add.country.func()
, which returns a data frame, on every file whose name ends with "sav".
my.results <- mclapply(list.files(path = my.path, pattern = "*.sav"), function(file.name){ my.add.country.func(file.name) })
If you need to combine the list of resulting data frames to one data frame, try:
my.df <- data.frame() for(i in 1:length(my.results)){ my.df <- rbind(my.df, my.results[[i]]) }