7

The problem

When running R outside of RStudio, plots will by default be shown in a pop-up window, e.g. provided by the quartz device on macOS, the X11 device on Unix, the windows device on Windows.

A special feature of these 'interactive' devices is that manually resizing the plot window causes the plots to be redrawn to fit the new dimensions. This feature is really useful!

A downside of the default interactive devices is that they're relatively slow. The {ragg} package provides alternative graphics devices like ragg::agg_png(), which render noticeably faster than the default devices, and often look better too. Unfortunately, these devices aren't responsive to resizing - you have to manually specify the dimensions of the plot before rendering.

In RStudio it's possible to use {ragg} as a backend interactively. In this case, the plot preview is rendered by {ragg}, and resizing the pane causes the preview to be re-rendered. I assume this is powered by RStudio magic behind the scenes, not by R.

What I want

I would like to achieve automatic resizing with a custom device outside of RStudio. I want my plots to be rendered/drawn by {ragg}, to appear in a floating window, and to be re-drawn when I resize this window.

What I've tried

I was hoping the default quartz device would allow me to specify a backend to draw the plot itself, but I don't think this is possible.

I was able to create an imitation of an interactive device powered by {ragg} using the system's default png viewer - the major downside of this approach is that there's no redrawing of the plot when I resize the window:

options(device = function() {
  file <- tempfile("last_plot_", fileext = ".png")

  ragg::agg_png(file, height = 480 * 5, width = 480 * 5, scaling = 5)

  withr::defer(
    {
      dev.off()
      browseURL(file)
    }, 
    sys.frame(1)
  )
})
6
  • 4
    I’ve never used it but the ‘httpgd’ package might be a building block to achieve this. Apr 10 at 11:43
  • 1
    Update: been playing with this a bit but struggling to get anything working reliably enough to not be frustrating in day-to-day use. Will post an edit if that changes.
    – wurli
    Apr 17 at 9:38
  • 1
  • 1
    Mike FC's rstudio::conf(2022) talk on R graphics has been helping a bit with this. Tl;dr is that X11 (available for mac but not until you install it) is really fast.
    – wurli
    Apr 18 at 11:20
  • 1
    Did you see this coolbutuseless.github.io/2019/10/03/…? Haven't studied it, but it looks it allows to create your custom device.
    – nicola
    2 days ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.