Kevin wrote a clever defer() function in a pull request two months ago, and it almost killed me when I first saw it.1 It looked so short, but I’m really bad at reasoning code that uses a combination of substitute(), evalq(), and do.call() with the envir argument, especially when substitute() is used three times, and there are three envir arguments.

I was once confused by on.exit(), so the defer() function was completely magic to me. Today I found that I needed something similar – I wanted to insert an on.exit() call to a parent function. I know how on.exit() works in the current function and have used it many times before, but I have never tried to inject on.exit() to a parent function, so I looked at Kevin’s code again, and simplified it a little bit (hopefully):
f = function() {
g()
print('printing in f()') # <- 2
}
g = function() {
x = 'an exit call added to f() by g()'
on.exit(print('exiting g()'), add = TRUE) # <- 1
do.call(
on.exit, list(substitute(print(x)), add = TRUE),
envir = parent.frame() # the key is this envir
) # <- 3
}
f()
# [1] "exiting g()"
# [1] "printing in f()"
# [1] "an exit call added to f() by g()"
You can see that first g() exits, then the print() expression in f() is executed, and finally f() exits, with the exit call added by g() executed.
I was looking for this because I have several functions that should do the same thing on exit, and I don’t want to repeat the code on.exit() in all these functions. It seems do.call(on.exit, envir = parent.frame()) solved my problem.
-
This PR is one of the greatest contributions in the history of R packages. I’ll talk about it next year. Some of you may be sleepless because of it. ↩︎
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).