I would like to know if it would be possible to output two different objects after using foreach
%dopar%
loop.
I will try to explain what I am looking for. Let's suppose I have two data.frames as a result of several operations inside the 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 output would be a list of data.frames of length 2, such as:
dim(result[[1]]) # equals to nrow=length(vec1) and ncol=100
dim(result[[2]]) # equals to nrow=length(vec2) and ncol=100
I have tried with this from a previous post Saving multiple outputs of 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 it doesn't 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 obtain only the data.frame of vec2
. Is there any way of returning or saving both outputs?
Thanks