How to make maps in R with parallel processing?

Asked

Viewed 216 times

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?

1 answer

1

There is probably some map that is giving problem when rotating. Only then you end up losing what you have already done. I recommend you use a function that does not stop the execution when you have some error.

When I use codes in parallel use the following workflow:

On Windows:

library(doSNOW)
library(foreach)
cl <- makeCluster(2)
registerDoSNOW(cl)

On Linux:

library(doMC)
registerDoMC(2)

Next:

plyr::l_ply(lista_sp, plyr::failwith(NULL, function(i, limite){
  mapas(i, limite = am_sul)
}), .parallel = T, limite = am_sul)

The function failwith modifies the function so that it returns a predefined value (in this case NULL) if an error occurs.

That way, at the end of the loop execution you can see in detail why some cases did not run.

However, seeing that your three nodes are giving error, it is worth it for you to check if the maps function is working for the first element of the lista_sp.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.