I am trying to run a function in parallel in a linux version of R. However, I keep finding that each parallel iteration returns NULL rather than the output of the function. I have isolated the problem to a for loop.
Here is a simplified version:
library(parallel)
rows <- seq(1,9,1)
for_test <- function(i){
for (s in 1:3){
print(i*s)
}
}
cls <- makeCluster(length(rows), type = 'FORK')
parLapply(cls, rows, for_test)
stopCluster(cls)
This returns NULL for each of the nine iterations, but changing the for_test function to:
for_test <- function(i){
s <- 2
print(i*s)
}
returns the result of i*s rather than NULL. I have run for loops within parallel previously and can't seem to figure out why it isn't working on this occasion. Is anyone able to point to where I am going wrong?