3
I’ve been trying to make multiple maps at the same time R using plot
and parallel processing using the package snowfall
. I did the following function mapas
:
mapas <- function (tx,
limite)
{
# Open o png
png(filename=paste0("mapas_mpc/",tx,".png"),width = 1365, height = 1024)
plot.new()
par(mar=c(0,0,0,0),oma=c(0,0,0,0))
# Plot South America border
#map(regions = SA, fill = TRUE, col="gray", border="black")
plot(limite, col="gray", border="black")
# Load species shapefile
tx_shp <- shapefile (paste0("MPC/",tx,".shp"))
# Plot species shapefile
plot(tx_shp,col=rgb(0/255,139/255,0/255,alpha=0.5), border="darkgreen" ,lwd=3,add=T)
# Species name
nome_sp <- gsub(pattern="_",replacement=" ",tx)
# Plot species name
text(x=-97,y=-27, nome_sp,font=4,cex=1.5)
# Close PNG
dev.off()
cat(paste("Mapa de ",tx),"\n")
}
After that I start processing in parallel:
library(snowfall)
# Strat parallel processing
sfInit(parallel=T,cpus=3)
And then I turn the function mapas
in parallel:
sfLapply(x = lista_sp,fun = mapas,limite = am_sul )
"lista_sp": is a vector os characters which contains the Names of Species. "am_sul": is the shapefile of South America boundaries.
Hence I have the following mistake:
Error in checkForRemoteErrors(val) : 3 nodes produced errors; first error: cannot coerce type 'S4' to vector of type 'double'
I don’t want my maps to be on the same page as Plot, because they are thousands. And I need to make the maps as fast as possible, so I chose parallel processing. How can I solve this problem?