baptiste / gridExtra

Miscellaneous Functions for "Grid" Graphics
http://cran.r-project.org/web/packages/gridExtra/index.html
15 stars 4 forks source link

marrangeGrob may fail in RStudio #6

Closed fabian-s closed 9 years ago

fabian-s commented 9 years ago

Calling marrangeGrob in RStudio always yields:

Warning message:
In (function ()  : Only one RStudio graphics device is permitted

and does not have the desired result if multiple pages are necessary -- pages simply get rendered on top of each other, so only the last one is fully visible.

In my code for the update of package spikeSlabGAM for gridExtra 2.0, I have "solved" this by doing:

# function definition contains argument interactive.dev = c("x11", "quartz", "windows")
# plotList: list of ggplots to render
# nrow, ncol as in marrangeGrob

interactive.dev <- match.arg(interactive.dev)
rstudio <- options()$device == "RStudioGD"

if(dev.interactive()){
  rstudio <- options()$device == "RStudioGD"
  if(rstudio & nrow*ncol < length(plotList)) {
    message("Temporarily setting <options(device='",interactive.dev,"')> to open multiple graphics displays since dispatch on multiple graphic devices is not supported in RStudio.")
    options(device=interactive.dev)
    on.exit(options(device="RStudioGD"))
  }
}

Maybe something like this could be useful inside the package itself as well. Of course the active device still remains the one opened last by marrangeGrob despite the on.exit-code and subsequent figures are rendered there and not in the RStudio graphics window, but: that's life... :hankey:

baptiste commented 9 years ago

Yes, I noticed that Rstudio doesn't play well with multiple pages. Basically, I consider it a bug in Rstudio, it should follow the standard behaviour of dev.new. I believe there was a recent thread on r-devel / r-help where JJ said that they fixed some unusual behaviour of Rstudio's interactive device. Maybe it works now, I need to check.

baptiste commented 9 years ago

Yes. I just tested the preview version of Rstudio, and it opens a quartz() window for the second plot, no error anymore. In the long-run this won't be an issue. For now though, we might have to leave with a few complaints, I don't think it's worth adding temporary hacks for this.

baptiste commented 9 years ago

similar thing with ggsave(), by the way. You may notice that it doesn't work with the current release of ggplot2, but the dev version should accept gtables.

fabian-s commented 9 years ago

alright, makes sense. thanks for the prompt reply.