I am trying to use foreach with the parallel backend to speed up computation (of cross validation of an {AUCRF} random forest for feature selection, if this does matter). In the process of doing so i need to get a subset of a vector. The name of the vector can change but is accessible as character vector. I used the eval(parse()) construct(good idea?) to get a subset of the vector.
Example:
library(parallel)
library(foreach)
library(stats)
#create cluster
clu <- makeCluster(detectCores() - 1)
registerDoParallel(clu, cores = detectCores() - 1)
bar<-c("a","b","c","d")
rab<-c(2,3)
bar.name<-"bar"
#expected output in this example is a list containing ten times
bar[rab]
#or
eval(parse(text=paste(bar.name,"[rab]",sep="")))
foo<-foreach(m = 1:10, .packages = c("stats")) %dopar% {
sink("foreach.txt")
print(bar.name)
print(parse(text=paste(bar.name,"[rab]",sep="")))
print(eval(parse(text=paste(bar.name,"[rab]",sep=""))))
foo.temp<-eval(parse(text=paste(bar.name,"[rab]",sep="")))
return(foo.temp)
}
sink()
stopCluster(clu)
However i get the following error:
Error in { : task 1 failed - "Object 'bar' not found"
I thought that each worker is getting a copy of the workspace with all objects. Any idea what I'm doing wrong?