Skip to content

[R-pkg-devel] use of `dev.new` across platforms in RStudio (Martin Maechler)

4 messages · Alex Chubaty, JJ Allaire, Martin Maechler

#
Currently the RStudio graphics device creation function fails if the
RStudio device is already active. We will modify this function to
rather than fail just delegate to the platform-appropriate device
creation function (which will create an external windows, X11, or
quartz graphics device window). This will make dev.new work as
expected within RStudio.

J.J. Allaire
#
Thank you Martin for passing this along to RStudio, and thank you JJ for
your reply.

JJ, do you have an estimated timeframe for when this fix will appear?
There's still the issue of RStudio users not updating their software at
each and every release, so it may time some time for `dev.new` to work for
most people.

In the mean time, the best work around I've come up with is as follows. It
checks for RStudio graphics device being set in options and basically
overrides it. The catch is that `setDevice` only works if invoked by the
user in the RStudio console. `unsetDevice` can be put into my package
`.onUnload()`. R CMD check doesn't seem to complain, but I'm not completely
satisfied with this workaround.

setDevice <- function() {
   options(myPackage.device = getOption("device"))  # move this line
to `.onLoad`
   if (getOption("device")=="RStudioGD") {
     if (Sys.info()["sysname"]=="Darwin") {
       options(device="quartz")
     } else if (Sys.info()["sysname"]=="Linux") {
       options(device="x11")
     } else if (Sys.info()["sysname"]=="Windows") {
       options(device="windows") # `dev.new` works on Windows, so
could omit this
     }
   }
  message("Default plot device set to ", getOption("device"), ". ",
          "Remember to reset your default device using `unsetDevice()`.")
}
# unset default graphics device (i.e., return to user's prev setting).
call during `.onUnload`?unsetDevice <- function() {
  if (!is.null(getOption("myPackage.device"))) {
    options(device=getOption("myPackage.device"))
  }
  options(myPackage.device = NULL)
}



Thoughts and suggestions welcome.

Thank you,
Alex

--
amc
On Sun, Jun 14, 2015 at 4:41 AM, JJ Allaire <jj at rstudio.com> wrote:

            

  
  
#
On Sun, Jun 14, 2015 at 3:46 PM, Alex Chubaty <alex.chubaty at gmail.com> wrote:
This is in our daily builds now and will also be included in our next
minor patch release (within a few weeks).

J.J.
#
>> I have neither been involved in the creation of the
    >> 'noRStudioGD' option, but from looking at dev.new() it is
    >> clear that it *should* work on all three platforms
    >> (Windows, Mac, Linuxen).
    >> 
    >> It is really unfortunate that this still does not work
    >> reliably (or stopped working with a new release of
    >> "something" ?)...
    >> 
    >> Honestely, I believe it is primarily RStudio's
    >> responsibility to ensure that something standard R as
    >> dev.new() keeps working in their interface to R,= and if
    >> it does not, at least contact the R developers (R core)
    >> about it.

    > Currently the RStudio graphics device creation function
    > fails if the RStudio device is already active. We will
    > modify this function to rather than fail just delegate to
    > the platform-appropriate device creation function (which
    > will create an external windows, X11, or quartz graphics
    > device window). This will make dev.new work as expected
    > within RStudio.

    > J.J. Allaire

Thanks a lot indeed, J.J. ... for such a quick and positive
reaction!

In a perfect world we would have communicated about and solved
the issue long ago.....  and I'm sorry that you've not been
contacted about the issue earlier.

Best regards,
Martin