Is there a way to open a graphics device that plots entirely to an array or raster in memory? I'd prefer it to use base graphics, but grid would be fine if it makes a difference. For an explicit example, I'd like to do the equivalent of this: filename <- tempfile(fileext = ".png") png(filename) plot(1:10, 1:10) dev.off() library(png) img <- readPNG(filename) unlink(filename) which puts the desired plot into the array `img`, but I'd like to do it without needing the `png` package or the temporary file. A possibly slightly simpler request would be to do this only for plotting text, i.e. I'd like to rasterize some text into an array. Duncan Murdoch
On Sun, May 28, 2023 at 1:46?PM Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> > Is there a way to open a graphics device that plots entirely to an array > or raster in memory? I'd prefer it to use base graphics, but grid would > be fine if it makes a difference. > > For an explicit example, I'd like to do the equivalent of this: > > filename <- tempfile(fileext = ".png") > png(filename) > plot(1:10, 1:10) > dev.off() > > library(png) > img <- readPNG(filename) > > unlink(filename) >The magick package has this capability too: library(magick) img <- magick::image_device() plot(1:10, 1:10) dev.off() image_data(img)
Perhaps the ragg package? That has an `agg_capture` device "that lets you access the device buffer directly from your R session." https://github.com/r-lib/ragg HTH, Jan On 28-05-2023 13:46, Duncan Murdoch wrote:> Is there a way to open a graphics device that plots entirely to an array > or raster in memory?? I'd prefer it to use base graphics, but grid would > be fine if it makes a difference. > > For an explicit example, I'd like to do the equivalent of this: > > ? filename <- tempfile(fileext = ".png") > ? png(filename) > ? plot(1:10, 1:10) > ? dev.off() > > ? library(png) > ? img <- readPNG(filename) > > ? unlink(filename) > > > which puts the desired plot into the array `img`, but I'd like to do it > without needing the `png` package or the temporary file. > > A possibly slightly simpler request would be to do this only for > plotting text, i.e. I'd like to rasterize some text into an array. > > Duncan Murdoch > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.