Saturday, May 2, 2015

Graphics Window for MacOS and RStudio Server

It's been a recurring headache to get graphics windows to open seamlessly in MacOS and Windows, running RStudio locally and RStudio Server. A solution has been proposed by Professor David Zeitler. He has modified the openGraph function in the DBDA2E-utilities.R script. If you use a Mac, or you use RStudio Server, please check it out and let us know whether or not it works for you by entering a comment at the bottom of this post. To make the change, just open DBDA2E-utilities.R in an editing window. Comment out the current definition of openGraph (don't delete it, in case you need to recover the original version). Insert the new function definition listed below, and be sure to save the edited version. Then source the script and try using openGraph or any existing script that calls it.

Update: See the comments for updated versions!

openGraph = function( width=7 , height=7 , mag=1.0 , ... ) {
  if( class( try(RStudioGD(),silent=TRUE) ) == "try-error" ) {
    # Not in RStudio, use graphic windows
    if ( .Platform$OS.type != "windows" ) { # Mac OS, Linux
      if ( .Platform$GUI != "AQUA" ) { # Linux
        tryInfo = try( X11( width=width*mag , height=height*mag , 
                            type="cairo" , ... ) )
        if ( class(tryInfo)=="try-error" ) {
          lineInput = readline("WARNING: Previous graphics windows will be closed because of too many open windows.\nTO CONTINUE, PRESS <ENTER> IN R CONSOLE.\n")
          graphics.off() 
          X11( width=width*mag , height=height*mag , type="cairo" , ... )
        }
      } else {
        # Mac OS - use quartz device
        tryInfo = try( quartz(width=width*mag , height=height*mag ,
                              ... ) )
        if ( class(tryInfo)=="try-error") {
          if ( class(tryInfo)=="try-error" ) {
            lineInput = readline("WARNING: Previous graphics windows will be closed because of too many open windows.\nTO CONTINUE, PRESS <ENTER> IN R CONSOLE.\n")
            graphics.off() 
            quartz( width=width*mag , height=height*mag , ... )
          } 
        }
      }
    } else { # Windows OS
      tryInfo = try( windows( width=width*mag , height=height*mag , ... ) )
      if ( class(tryInfo)=="try-error" ) {
        lineInput = readline("WARNING: Previous graphics windows will be closed because of too many open windows.\nTO CONTINUE, PRESS <ENTER> IN R CONSOLE.\n")
        graphics.off() 
        windows( width=width*mag , height=height*mag , ... )    
      }
    }
  }
}


Thank you, David!

4 comments:

  1. Before sending this to John, I'd tried it in RStudio on
    my iMac, RStudio Server running on my linux machine
    and using the RGui console on the iMac. If anyone has
    problems, feel free to contact me (zeitlerd@gvsu.edu)
    and I'll see if I can help.

    ReplyDelete
  2. Here's an update to the above code. It uses a cleaner way of testing for RStudio and reduces the size of text for cleaner graphics being displayed in RStudio. Note that when running in RStudio, we don't have as much control of the graphics window and so there will be more problems than with the external graphics windows John's code uses. Sometimes just using the zoom button on the RStudio plot panel cleans up a lot of issues.

    openGraph = function( width=7 , height=7 , mag=1.0 , ... ) {
    if( class( try(rstudio::versionInfo(),silent=TRUE) ) == "try-error" ) {
    # Not in RStudio, use graphic windows
    if ( .Platform$OS.type != "windows" ) { # Mac OS, Linux
    if ( .Platform$GUI != "AQUA" ) { # Linux
    tryInfo = try( X11( width=width*mag , height=height*mag ,
    type="cairo" , ... ) )
    if ( class(tryInfo)=="try-error" ) {
    lineInput = readline("WARNING: Previous graphics windows will be closed because of too many open windows.\nTO CONTINUE, PRESS IN R CONSOLE.\n")
    graphics.off()
    X11( width=width*mag , height=height*mag , type="cairo" , ... )
    }
    } else {
    # Mac OS - use quartz device
    tryInfo = try( quartz(width=width*mag , height=height*mag ,
    ... ) )
    if ( class(tryInfo)=="try-error") {
    if ( class(tryInfo)=="try-error" ) {
    lineInput = readline("WARNING: Previous graphics windows will be closed because of too many open windows.\nTO CONTINUE, PRESS IN R CONSOLE.\n")
    graphics.off()
    quartz( width=width*mag , height=height*mag , ... )
    }
    }
    }
    } else { # Windows OS
    tryInfo = try( windows( width=width*mag , height=height*mag , ... ) )
    if ( class(tryInfo)=="try-error" ) {
    lineInput = readline("WARNING: Previous graphics windows will be closed because of too many open windows.\nTO CONTINUE, PRESS IN R CONSOLE.\n")
    graphics.off()
    windows( width=width*mag , height=height*mag , ... )
    }
    }
    } else {
    # setup for RStudio
    par( cex=0.7, ... )
    }
    }

    ReplyDelete
  3. This is a lifesaver! We just spent hours trying to get RStudio Server to play nicely with openGraph. This solves everything. Thanks!

    ReplyDelete
  4. Hi,
    In my version of RStudio, there is no rstudio package. So the test does not work.

    In the version of the script in DocZee comment above, I changed the line

    if( class( try(rstudio::versionInfo(),silent=TRUE) ) == "try-error" ) {

    by

    if( class( try(RStudio.Version(),silent=TRUE) ) == "try-error" ) {

    and it works fine for me.

    ReplyDelete