rforeach

R: "Object of type 'S4' is not subsettable" when using foreach loop


I deal with some shapefiles and rasters.

When I execute my script line by line (or part by part), everything runs as expected. However, if I execute it as a whole (either source it or STRG+A and then STRG+ENTER), it throws an error in the following section:

# ... some code

list = list()

list = foreach(i = seq(from = 9, to = 80, by = 5)) %dopar% {
    
    df[which(df@data$column.name > i), ] 
} 

# ... some code

Error message: Error in { : task 2 failed - "Object of type 'S4' is not subsettable"

Where fishnet is a SpatialPolygonsDataFrame. Code subsets my SpPolDaFr so I get 15 subsetted SpPolDaFr written in the list.

I was thinking of maybe being foreach the reason. However, I have other foreach calls a priori which run fine. I do the doParallel loop because my SpPolDaFr is 11 GB in size to speed things up.


Solution

  • You usually get this kind of error when the workers haven't loaded the package that defines the class of one of the variables. If the class of "df" is "SpatialPolygonsDataFrame" which is defined by the "sp" package, then you should use the foreach .packages="sp" option so the workers will be able to properly operate on "df".