Intro

RStudio is a great integrated developer environment (IDE) for R. I’m constantly recommending it to not only students but also faculty. The primary reason for this being: I am infatuated with how feature rich it is… Except for one, itsy bitsy feature… the plot window / graphs. Shudders. Not only is it inconvenient to have to switch back and forth between graphs you wish to compare, it is especially problematic with ggplot2 on OS X since more often than not, I receive the following error that there appears to be no known fix for:

Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
polygon edge not found

Now, you might say, “Well, Coatless, have you tried using par(mfrow=c(1,x))) or grid.arrange()?” Yes, but that is not a valid strategy as the information becomes “crowded”. Hence, why this post came into being…

Come in RStudio, this is mission control…

In order to make sure the external function is activated appropriately, e.g. within RStudio, we first must know if R is being run from RStudio. To achieve this, we check to see if the .Platform list has the GUI set to use RStudio or a different GUI (e.g. Unix (“X11”, “Tk”), “AQUA” (running under R.app on OS X), “Rgui” and “RTerm” (Windows)).

#' @title Is R Open in RStudio?
#' @description 
#' Detects whether R is open in RStudio. 
#' @return 
#' A \code{logical} value that indicates whether R is open in RStudio.
#' @author JJB
#' @examples
#' is.rstudio()
is.rstudio = function(){
  .Platform$GUI == "RStudio"
}

Reassigning the Graphing Device for RStudio

With this in hand, we can write a function that manipulates the device value in options(). To pop open a new graphing window by default, the device value must be changed away from RStudio’s default setting: RStudioGD. Furthermore, the graphing device itself must be killed and remade using graphics.off(). Sadly, the killing of the graphing device is not ideal as it will remove any previously graphed items from history. Alas, it is a low price to pay for freeing graphics.

#' @title Change Default Graphing Device from RStudio
#' @description 
#' Checks to see if the user is in RStudio. If so, then it changes the device to a popup window. 
#' @param ext A \code{logical} indicating whether the graph should be done externally or internally in RStudio.
#' @details 
#' Depending on the operating system, the default drivers attempted to be used are:
#' 
#' OS X and Linux: quartz()
#' 
#' Windows: windows()
#' 
#' Note, this setting is not permanent. Thus, the behavioral change will last until the end of the session. 
#'
#' Also, the active graphing environment will be killed. As a result, any graphs that are open will be deleted. You will have to regraph them. 
#' @author JJB
#' @examples
#' \dontrun{
#' # Turn on external graphs
#' external_graphs()
#' 
#' # Turn off external graphs
#' external_graphs(F)
#' }
external_graphs = function(ext = TRUE){
  if( is.rstudio() ){
    if(isTRUE(ext)){
      o = tolower(Sys.info()["sysname"])
      a = switch(o,
                 "darwin"  = "quartz",
                 "linux"   = "x11",
                 "windows" = "windows")
      options("device" = a)
    } else{
      options("device"="RStudioGD")
    }
    
    # Kill open graphic devices
    graphics.off()
  }
}

Both of these functions have been implemented in the balamuta R package. Grab a copy via:

# Install dependencies
install.packages("devtools")

# Install the package from github
devtools::install_github("coatless/balamuta")

library("balamuta")

is.rstudio()
external_graphs()
external_graphs(F)

Happy Graphing outside of RStudio!