Applications of on.exit()

  • Set an R option
  • Set an environment variable
  • Change working directory
  • Create a file or directory
  • Create a resource on an external system
neat <- function(x, sig_digits) {
  op <- options(digits = sig_digits)
  on.exit(options(op), add = TRUE)
  print(x)
}

neat(pi, 2)
## [1] 3.1
pi
## [1] 3.141593