on.exit() Limitations

  • You should always call it with add = TRUE to ensure that the call is added to the list of deferred tasks (instead of replaces).

  • You should always call it with after = FALSE to ensure that the cleanup occurs in reverse order to setup.

  • It doesn’t work in the global environment for interactive checking.

  • You can’t wrap up on.exit() in a helper function.