R.devices – Into the Void

[This article was first published on JottR on R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R.devices 2.16.0 – Unified Handling of Graphics Devices – is on CRAN. With this release, you can now easily suppress unwanted graphics, e.g. graphics produced by one of those do-everything-in-one-call functions that we all bump into once in a while. To suppress graphics, the R.devices package provides graphics device nulldev(), and function suppressGraphics(), which both send any produced graphics into the void. This works on all operating systems, including Windows.

Guillaume Nery base jumping at Dean’s Blue Hole, filmed on breath hold by Julie Gautier

Examples

library(R.devices)
nulldev()
plot(1:100, main = "Some Ignored Graphics")
dev.off()

R.devices::suppressGraphics({
  plot(1:100, main = "Some Ignored Graphics")
})

Other Features

Some other reasons for using the R.devices package:

  • No need to call dev.off() – Did you ever forgot to call dev.off(), or did a function call produce an error causing dev.off() not to be reached, leaving a graphics device open? By using one of the toPDF(), toPNG(), … functions, or the more general devEval() function, dev.off() is automatically taken care of.

  • No need to specify filename extension – Did you ever switch from using png() to, say, pdf(), and forgot to update the filename resulting in a my_plot.png file that is actually a PDF file? By using one of the toPDF(), toPNG(), … functions, or the more general devEval() function, filename extensions are automatically taken care of – just specify the part without the extension.

  • Specify the aspect ratio – rather than having to manually calculate device-specific arguments width or height, e.g. toPNG("my_plot", { plot(1:10) }, aspectRatio = 2/3). This is particularly useful when switching between device types, or when outputting to multiple ones at the same time.

  • Unified API for graphics options – conveniently set (most) graphics options including those that can otherwise only be controlled via arguments, e.g. devOptions("png", width = 1024).

  • Control where figure files are saved – the default is folder figures/ but can be set per device type or globally, e.g. devOptions("*", path = "figures/col/").

  • Easily produce EPS and faviconstoEPS() and toFavicon() are friendly wrappers for producing EPS and favicon graphics.

  • Capture and replay graphics – for instance, use future::plan(remote, workers = "remote.server.org"); p %<-% capturePlot({ plot(1:10) }) to produce graphics on a remote machine, and then display it locally by printing p.

Some more examples

R.devices::toPDF("my_plot", {
  plot(1:100, main = "Amazing Graphics")
})
### [1] "figures/my_plot.pdf"

R.devices::toPNG("my_plot", {
  plot(1:100, main = "Amazing Graphics")
})
### [1] "figures/my_plot.png"

R.devices::toEPS("my_plot", {
  plot(1:100, main = "Amazing Graphics")
})
### [1] "figures/my_plot.eps"

R.devices::devEval(c("png", "pdf", "eps"), name = "my_plot", {
  plot(1:100, main = "Amazing Graphics")
}, aspectRatio = 1.3)
### $png
### [1] "figures/my_plot.png"
### 
### $pdf
### [1] "figures/my_plot.pdf"
### 
### $eps
### [1] "figures/my_plot.eps"

See also

To leave a comment for the author, please follow the link and comment on their blog: JottR on R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)