- Update on 2020-06-23
-
The issue mentioned in this post has been fixed in R 4.0.2, thanks to Martin Maechler.
I have used on.exit() for several years, but it was not until the other day that I realized a very weird thing about it: you’d better follow the default positions of its arguments expr and add, i.e., the first argument has to be expr and the second has to be add.
on.exit(expr = NULL, add = FALSE)
If you do on.exit(add = TRUE, {...}), weird things can happen. I discovered this by accident. I have never switched the positions of expr and add before, and I was surprised that R CMD check failed on Travis with an error message that confused me in the beginning:
Error in on.exit(add = TRUE, if (file.exists(main)) { :
invalid 'add' argument
I was thinking why add = TRUE was considered invalid. Then I guessed perhaps the expression if (file.exists(main)) {} was treated as the actual value of add. So I switched to the normal order of arguments, and the error was gone.
I tested it a bit more and was totally confused, e.g., why was 1 printed twice below? I guess TRUE was not printed because add was treated as expr.
f = function() {
on.exit(add = print(TRUE), print(1))
}
f()
# [1] 1
# [1] 1
I don’t have the capability to understand the source code in C, and I’ll leave it experts to explain the weird things I observed. For me, I’ll just never move add before expr again.
BTW, I don’t know the rationale for the default add = FALSE in on.exit(), but I have not used add = FALSE for a single time, so I feel add = TRUE might be a better default. When I want to do something on exit, I almost surely mean do it in addition to the things that I assigned to on.exit() before, instead of cleaning up all previous tasks and only doing this one (add = FALSE).
- Update on 2018-01-17
-
Half a year later, I was bitten by the same problem again in the tinytex package. Never, ever, do
on.exit(add = TRUE, expr).
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).