- Update on 2024-07-04
-
Since Mehrad discovered and mentioned this post on Mastodon, I want to point out that the null device has been officially introduced into R since v2.15.0 (2012-03-30), so you can set:
options(device = function(...) { pdf(NULL, ...) })The
R_GD_nullDevicewas said to be removed in the future, so you’d better not use it any more.
It is well-known that R has several graphics devices – either the screen devices (X11(), windows(), …) or the off-screen devices (pdf(), png(), …). We can query the default graphics device in options():
getOption('device')
In a non-interactive session, the default device is pdf(). This is why Sweave has to create a file named Rplots.pdf no matter if you want it or not when you run Sweave on an Rnw file which has code chunks creating plots. Such a behaviour is annoying to me – the PDF file is not only unnecessary, but also time-consuming (creating this PDF file is completely a waste of time). Is there a way to set a “null” device? (like the /dev/null for *nix users) The answer is yes, but not so obvious. I have not found the device below documented anywhere:
options(device = function(...) {
.Call("R_GD_nullDevice", PACKAGE = "grDevices")
})
This device can speed up Sweave a lot when there are many plots to draw. Here is a comparison:
x = rnorm(1000)
system.time({
.Call("R_GD_nullDevice", PACKAGE = "grDevices")
replicate(500, plot(x, pch = 1:21))
dev.off()
})
# user system elapsed
# 1.51 0.02 1.53
system.time({
pdf(file.path(tempdir(), "Rplots.pdf"))
replicate(500, plot(x, pch = 1:21))
dev.off()
})
# user system elapsed
# 47.81 0.20 48.10
One thing I don’t understand in Sweave is that it evaluates the code chunk twice if its Sweave options contain fig=TRUE. I think this might be a waste of time as well, and this is why I like pgfSweave, which has both the mechanism of caching R objects (using cacheSweave) and a smart way to cache graphics (using pgf).
WARNING: this null device may not work with plots that contain (math) expressions! (take a look at demo(plotmath) in case you do not know what are expressions in R graphics)
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).