The function as_grob()
needs to open a graphics device to render ggplot objects into
grid graphics objects. Unfortunately, there is no universally reliable graphics device available
in R that always works. Therefore, this function allows you to switch out the null device.
set_null_device(null_device)
Either a string that defines the null device ("pdf", "png", "cairo", "agg") or a function that returns a new graphics device.
You need to be aware that some graphics devices cause side effects when used as null devices.
If you use an interactive device as null device, you may see an empty plot window pop up. Similarly,
if you use a graphics device that writes a file, then you may find temporary files associated
with the device. The default null device, pdf(NULL)
, does not cause these side effects. However, it has
has other limitations. For example, on OS X, it cannot use all the fonts that are available on the
system. The ragg device can use all fonts, but it will create temporary files.
Available null devices are: pdf_null_device()
, png_null_device()
,
cairo_null_device()
, agg_null_device()
set_null_device("png") # set the png null device
# create a jpeg null device
jpeg_null_device <- function(width, height) {
jpeg(
filename = tempfile(pattern = "jpeg_null_plot", fileext = ".jpg"),
width = width, height = height, units = "in", res = 96
)
dev.control("enable")
}
set_null_device(jpeg_null_device)