Search notes:

R function: on.exit

on.exit records a function to be called when the current function exits.
Note that the function is not evaluated when on.exit() is called.
f_two <- function() {
  on.exit(print('f_two exited'));

  print('I am in f_two');
}

f_one <- function() {
  on.exit(print('f_one exited'));

  print('I am in f_one and am going to call f_two');
  f_two();
}

f_one();
#
#  I am in f_one and am going to call f_two
#  I am in f_two
#  f_two exited
#  f_one exited
Github repository about-r, path: /functions/on.exit.R

See also

Index to (some) R functions

Index