[Rd] WISHLIST: on.exit(..., add=TRUE, where="first") to address common use cases

Henrik Bengtsson hb at biostat.ucsf.edu
Sun Nov 3 22:42:08 CET 2013


Before trying to submit a patch(*) to on.exit(), I'd like to check
whether there is an interest in enhancing on.exit(..., add=TRUE) such
that it is possible to specify whether the added expression should be
added before or after already recorded expression.  The default is now
to add it after, but it would often be useful to add it before
previously recorded expressions.

EXAMPLE:

foo <- function(path="work") {
  # Change working directory. Make sure to reset on exit.
  opwd <- setwd(path)
  on.exit(setwd(opwd))

  # Write to a local temporary file.  Make sure to remove it on exit.
  cat("Hello", file="local.txt")
  on.exit(file.remove("local.txt"), add=TRUE, where="first")
}

Without where="first" (i.e. using where="last" as on.exit() does now),
it all becomes unnecessarily complicated.

Comments?

(*) It seems to come down to adjusting a few lines of code to
do_onexit() [http://svn.r-project.org/R/trunk/src/main/builtin.c] to
control whether the expression should be prepended or appended to the
existing set of recorded expressions.

/Henrik



More information about the R-devel mailing list