Arthur
Arthur

Reputation: 376

How to avoid argument name collision in R snow/ snowfall package, sfSapply?

I'm trying to use the snow and snowfall packages, specifically the sfSapply() function to extract data from multiple raster files. It looks something like this:

queue <- list(rast1, rast2, rast3)
sfInit(parallel=TRUE, cpus=3)
sfLibrary(raster)
sfLibrary(rgdal)
sfLibrary(sp)
a <- sfSapply(queue, extract, sp=TRUE, fun=mean, y=tracts)
sfStop()

The fun argument passed to sfSapply() is intended for the extract() function (in the raster library). However, sfSapply() also takes a fun argument (extract()); in this example, I've provided it as the second positional argument.

How can I specify a fun argument for the passed function and not have it confused with the fun argument expected by sfSapply()?

Upvotes: 0

Views: 228

Answers (1)

Lucas Fortini
Lucas Fortini

Reputation: 2480

One workaround is to create a custom extract function that has these arguments built in:

sfRasterExtract=function(raster_obj){
extract(raster_obj, sp=TRUE, fun=mean, y=tracts)
}

Make sure to use sfExportAll() after sfInit to import the function to all instances you will use, then run

a <- sfSapply(queue, sfRasterExtract)

Upvotes: 1

Related Questions