R.devices – Into the Void
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 causingdev.off()
not to be reached, leaving a graphics device open? By using one of thetoPDF()
,toPNG()
, … functions, or the more generaldevEval()
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 amy_plot.png
file that is actually a PDF file? By using one of thetoPDF()
,toPNG()
, … functions, or the more generaldevEval()
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
orheight
, 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 favicons –
toEPS()
andtoFavicon()
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 printingp
.
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"
Links
- R.devices package:
- CRAN page: https://cran.r-project.org/package=R.devices
- GitHub page: https://github.com/HenrikBengtsson/R.devices
See also
- A Future for R: Slides from useR 2016, 2016-07-02
- See Slide 17 for an example of using
capturePlot()
remotely and plotting locally
- See Slide 17 for an example of using
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.