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!
Before sending this to John, I'd tried it in RStudio on
ReplyDeletemy 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.
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.
ReplyDeleteopenGraph = 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, ... )
}
}
This is a lifesaver! We just spent hours trying to get RStudio Server to play nicely with openGraph. This solves everything. Thanks!
ReplyDeleteHi,
ReplyDeleteIn 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.
This comment has been removed by the author.
ReplyDeleteI use ".Platform$pkgType".
ReplyDeleteopenGraph = function( width=7 , height=7 , mag=1.0 , ... ) {
if( class( try(RStudio.Version(),silent=TRUE) ) == "try-error" ) {
# Not in RStudio, use graphic windows
if ( .Platform$OS.type != "windows" ) { # Mac OS, Linux
if ( !(grepl("mac.binary", .Platform$pkgType ))) { # 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, ... )
}
}