I am trying to use a high performance cluster at my institution for the first time and I have hit a problem that I can't resolve.
The following code returns an error:
ptime<-system.time({
r <- foreach(z = 1:length(files),.combine=cbind) %dopar% {
raster <- raster(paste(folder,files[1],sep=""))
data<-getValues(raster)
clp <- na.omit(data)
for(i in 1:length(classes)){
results[i,z]<-length(clp[clp==classes[i]])/length(clp)
print(z)
}
}
})
Error in { : task 1 failed - "could not find function "raster""
A also tried a different foreach code for another task I have:
r <- foreach (i=1:length(poly)) %dopar% {
clip<-gIntersection(paths,poly[i,])
lgth<-gLength(clip)
vid<-poly@data[i,3]
path.lgth[i,] <- c(vid,lgth)
print(i)
}
and this time the gIntersection function isn't found. Obviously the packages are all installed and loaded. After reading some forum posts it seem it has to do with the environment that the functions execute/operate in.
Can someone please help? I'm not a programmer!
Thank you!
Update:
I have adjusted my code for the solution provided:
results<-matrix(nrow=length(classes),ncol=length(files))
dimnames(results)[[1]]<-classes
dimnames(results)[[2]]<-files
ptime<-system.time({
foreach(z = 1:length(files),.packages="raster") %dopar% {
raster <- raster(paste(folder,files[z],sep=""))
data<-getValues(raster)
clp <- na.omit(data)
for(i in 1:length(classes)){
results[i,z]<-length(clp[clp==classes[i]])/length(clp)
print(z)
}
}
})
But what I get is an output (my results matrix) filled with na's. As you can see I create a matrix object called results to fill with results (which works with for loops), but after reading the documentation for foreach it seems that you save your results differently with this function.
And advice on what I should choose for the .combine argument?