Where does on.exit() work?

on.exit() can be used on:

When does on.exit() run?

  • In functions, the code in on.exit() runs after all the rest of the code has run.

  • It runs even if there are errors / warnings.

  • You can have multiple calls to on.exit() inside a function (use add = TRUE, so a call doesn’t overwrite an earlier one)