pat-s
pat-s

Reputation: 6302

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.

Upvotes: 2

Views: 6924

Answers (2)

user468311
user468311

Reputation:

I'd like to suggest using different cluster type when registering the multithreading. Unlike the default type, FORK creates a copy of the process. There's no need to specify the packages, but FORK is only available on Unix. The code should look like this:

cl <- makeCluster(N_CORES, type = "FORK")
registerDoParallel(cl)  

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

stopCluster(cl)

Upvotes: 0

Steve Weston
Steve Weston

Reputation: 19677

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".

Upvotes: 6

Related Questions