I am trying to run a function using foreach
and %dopar%
that will pass its results back into itself for each iteration. Small example below:
require(doParallel)
test_function <- function(data)
{
result <- rbind(data, data)
return(result)
}
test_data <- mtcars
cl <- makeCluster(4)
registerDoParallel(cl)
results <- foreach(i = 1:10) %dopar%
{
aa <- test_function(test_data)
aa$iteration <- i
test_data <- aa
return(aa)
}
stopCluster(cl)
What I am hoping to see in results
is a list of ten data frames, each one sequentially doubling in number of rows.
It appears that re-defining test_data
within the foreach
function does not do this, as it would if I just ran these commands within a standard for loop - like so:
results <- list()
for(i in 1:10)
{
aa <- test_function(test_data)
aa$iteration <- i
test_data <- aa
results[[i]] <- aa
}
Would appreciate any insight into what I'm overlooking here.