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)
)
})