Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

exit_call

Call on.exit() in a parent function


Description

The function on.exit() is often used to perform tasks when the current function exits. This exit_call() function allows calling a function when a parent function exits (thinking of it as inserting an on.exit() call into the parent function).

Usage

exit_call(fun, n = 2, ...)

Arguments

fun

A function to be called when the parent function exits.

n

The parent frame number. For n = 1, exit_call(fun) is the same as on.exit(fun()); n = 2 means adding on.exit(fun()) in the parent function; n = 3 means the grandparent, etc.

...

Other arguments to be passed to on.exit().

References

This function was inspired by Kevin Ushey: https://yihui.org/en/2017/12/on-exit-parent/

Examples

f = function(x) {
    print(x)
    xfun::exit_call(function() print("The parent function is exiting!"))
}
g = function(y) {
    f(y)
    print("f() has been called!")
}
g("An argument of g()!")

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.