I have tried to make multiple maps at the same time in R using plot
and parallel processing using the snowfall
package. I have done 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 the parallel processing:
library(snowfall)
# Strat parallel processing
sfInit(parallel=T,cpus=3)
And then I run the mapas
function in parallel:
sfLapply(x = lista_sp,fun = mapas,limite = am_sul )
"list_p": is a vector the characters which contains the names of species. "am_sul": is the shapefile of South America boundaries.
Then I have the following error:
Error in checkForRemoteErrors(val) : 3 nodes produced errors; first error: cannot coerce type 'S4' to vector of type 'double'
I do not want my maps to be on the same plot page, even because they are thousands. I need to make maps as fast as possible, so I chose parallel processing. How can I solve this problem?