I would like to know if/how it would be possible to return multiple outputs as part of foreach dopar
loop.
Let's take a very simplistic example. Let's suppose I would like to do 2 operations as part of the foreach
loop, and would like to return or save the results of both operations for each value of i
.
For only one output to return, it would be as simple as:
library(foreach)
library(doParallel)
cl <- makeCluster(3)
registerDoParallel(cl)
oper1 <- foreach(i=1:100000) %dopar% {
i+2
}
oper1
would be a list with 100000 elements, each element is the result of the operation i+2
for each value of i.
Suppose now I would like to return or save the results of two different operations separately, e.g. i+2
and i+3
. I tried the following:
oper1 = list()
oper2 <- foreach(i=1:100000) %dopar% {
oper1[[i]] = i+2
return(i+3)
}
hoping that the results of i+2
will be saved in the list oper1
, and that the results of the second operation i+3
will be returned by foreach
. However, nothing gets populated in the list oper1
! In this case, only the result of i+3
gets returned from the loop.
Is there any way of returning or saving both outputs in two separate lists?
return(c(i+2,i+3))
? If you really need them in separate lists, you can do that afterforeach
returns. – Hypoblastreturn(list(i+2,i+3))
. – Hypoblast