2

I would like to use the R 'parallel' library to parallelize generating some plots using the 'ggplot2' library and have run into a snag when attempting to do this within RStudio. Within the IDE, mclapply alternately freezes the session, or fails to run ggsave() to write a plot to disk (no errors or warnings are given). It works perfectly 100% of the time when run outside of RStudio. I'm guessing that RStudio is doing something nasty with graphics devices, but I can't figure out what or a work around (I've tried png()/dev.off() too, same problem). Here's code that reproduces the problem:

library(ggplot2)
library(parallel)

mclapply(
    0:4,
    function(n) {
        df <- data.frame(x = runif(10), y = runif(10))
        p  <- ggplot(df, aes(x, y)) + geom_point()

        ggsave(
            paste0('mclapply-', n, '.png'),
            plot   = p,
            device = 'png',
            width  = 4,
            height = 4
        )

        return(n)
    }
)

Any suggestions for a work-around?

EDIT: R 3.4.4 + RStudio 1.1.419 + ggplot2 2.2.1 on macOS 10.13.4; mclapply() reverts lapply() on Windows (where it's not supported).

  • Unless something has changed, Windows reverts to using lapply() -- which works -- instead of mclapply(), which isn't supported on Windows (you need to use the cluster functions instead). I'm using macOS (High Sierra) -- I haven't tried Linux yet, but I will and see if the behavior is the same there. – James McIninch May 03 '18 at 13:55
  • I've tried it with Linux but it didn't work. You probably should file an issue https://github.com/rstudio/rstudio/issues – Tung May 03 '18 at 16:08
  • On R 3.5, I get this error message: "The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec()." So this is likely a fundamental limitation of R, and the fact that it works in some cases should be considered accidental. – Claus Wilke May 03 '18 at 22:15

1 Answers1

0

ggsave seems to work during parallel processing if you use the foreach package instead of mcapply:

library(foreach)

cl <- parallel::makeCluster(parallel::detectCores())
doParallel::registerDoParallel(cl)

foreach(n = 1:4, .packages = 'ggplot2') %dopar% {
   df <- data.frame(x = runif(10), y = runif(10))
   p  <- ggplot2::ggplot(df, aes(x, y)) + geom_point()

        ggplot2::ggsave(
        paste0('mclapply-', n, '.png'),
        plot   = p,
        device = 'png',
        width  = 4,
        height = 4
    )
    return(n)
}
parallel::stopCluster(cl)

This is under: macOS 10.13.6, R 3.5.1, and RStudio 1.1.453

divibisan
  • 11,659
  • 11
  • 40
  • 58