Outptut two objects using foreach

I would like to know if two different objects can be output after using the foreach %dopar% foreach .

I will try to explain what I am looking for. Suppose I have two data.frames as a result of several operations inside a loop:

 library(doMC) library(parallel) registerDoMC(cores=4) result <- foreach(i=1:100) %dopar% { #### some code here #### some code here vec1 <- result_from_previous code # It would be the 1st object I'd like to ouput vec2 <- result_from_previous code # It would be the 2nd object I'd like to output } 

My desired result would be a list of data.frames of length 2, for example:

 dim(result[[1]]) # equals to nrow=length(vec1) and ncol=100 dim(result[[2]]) # equals to nrow=length(vec2) and ncol=100 

I tried using this from a previous post Saving multiple outputs of a foreach dopar loop :

 comb <- function(x, ...) { lapply(seq_along(x), function(i) c(x[[i]], lapply(list(...), function(y) y[[i]]))) result <- foreach(i=1:100, .comb='comb', .multicombine=TRUE) %dopar% { #### some code here #### some code here vec1 <- result_from_previous code vec2 <- result_from_previous code list(vec1, vec2) } 

But this does not give the expected result.

When I do the following:

 result <- foreach(i=1:100, .comb=cbind) %dopar% { #### some code here #### some code here vec1 <- result_from_previous code vec2 <- result_from_previous code } 

I get only data.frame vec2 . Is there a way to return or save both outputs?

thanks

+7
foreach parallel-processing r
source share
1 answer

If you need to return two objects from the body of a foreach loop, you must somehow associate them with one object, and a list is the most common way to do this. The trick is to provide the appropriate combination function to achieve the desired end result. If you want to combine all vec1 objects with cbind , as well as all vec2 objects with cbind , the mapply function mapply very convenient. I think this is what you want:

 comb <- function(...) { mapply('cbind', ..., SIMPLIFY=FALSE) } 

Here is a small test program for this combined function:

 result <- foreach(i=1:100, .combine='comb', .multicombine=TRUE) %dopar% { vec1 <- rep(i, 10) vec2 <- rep(2*i, 10) list(vec1, vec2) } 

This will return a list containing two 10 X 100 matrices, but the same combining function can be used if vec1 and vec2 are data frames.

+10
source share

All Articles