I want to run the following code:
library(parallel)
cl <- makeCluster(detectCores())
requires <- c("fUnitRoots","fGarch")
for(req in requires) {
clusterEvalQ(cl,require(req))
}
list1 <- clusterApply(cl,1:10,function(i) {
x <- rnorm(100)
y <- rnorm(100)
m <- lm(y~x)
res <- resid(m)
t <- adfTest(res) ## this function is in {fUnitRoots}
return(t@test$statistic)
})
stopCluster(cl)
However, fUnitRoots package is not loaded in any node. It is probably because clusterEvalQ(cl,expr) where expr is an expression. require(req) is treated as an expression where req is not regarded as the iterator variable as a character.
How should I refine the code to make it work?