1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
library(ggplot2)
library(doParallel)
dt <- data.frame("a"=rnorm(100), "b"=rnorm(100))
cl <- makeCluster(5)
registerDoParallel(cl)
b <- c(T,T,F,F,T)
#lp <- list()
clusterExport(cl, c("dt", "b")) #,"lp"))
tryCatch(
{
lp <- foreach::foreach(i=1:5, .packages = c("pxlsar", "ggplot2")) %dopar%
{
if(b[i]==T) {
p <- ggplot(dt, aes(a,b))+ geom_point()+ labs(x = "", y="", title = paste0("plot",i))
return(p)
} else { message("F") }
} # fin foreach
}, error=function(e) { message(e); stopCluster(cl) }
) # fin tryCatch
stopCluster(cl)
gp <- gridExtra::grid.arrange(grobs = lp,ncol = 3,
bottom="x",
left="y") |
Partager