Changing graphics backend for unit testing

48 Views Asked by At

I have an R-package containing a function that generates a plot that relies on the AGG graphics backend to draw emoji. However, when I include this function in the unit tests, the tests fail because I can't set the AGG graphics backend (correctly) in the test environment. Tests need to pass to stay on CRAN.

On my machine, I can do it manually by going to Tools > Global options > General > Graphics > Backend > AGG. When I try to set this manually, I get the following error: Error in 'grid.newpage()': no active or default device

Here's what I tried so far:

test_that("Plotting Emoji", {

  OldDevice <- getOption("device")
  options(device = "AGG")

 # generating the ggplot2 and some data, do tests on the data

 options(device = OldDevice)

}

Is it possible to change the device temporarily from a script? If so, how would I do it and should this be done in the testing script or the function itself? Any help is much appreciated!

0

There are 0 best solutions below