How can I use libraries within a parallelized function with ply?

32 Views Asked by At

Novice programmer here. I am trying to use plyr's ldply function and doParallel to speed up a process, but my function uses the libraries terra and sp. My limited understanding of parallel processing is that I have to pass everything that the ldply function will need, because it cannot access outside objects while running in parallel. However, I can't figure out how to pass the necessary packages.

Here is my code right now:

cl<-makePSOCKcluster(3)
registerDoParallel(cl)

polygons <- 1:100
paddf <- ldply(polygons, .fun = rasterize_polygons, polysf = polysf, pgdf = pgdf, .parallel = T)

stopCluster(cl)

With this code, I get the error that the function terra::ext, inside my rasterize_polygons function, is not recognized. I tried passing the argument .packages = c("terra", "sf") into ldply, but then I got the error task 1 failed - "unused argument (.packages = c("terra", "sf", "gdal"))".

Any help would be very appreciated!

0

There are 0 best solutions below