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
WISHLIST: on.exit(..., add=TRUE, where="first") to address common use cases
2 messages · Henrik Bengtsson, William Dunlap
I used to worry about the order of evaluation of on.exit expressions and
thought that maybe we needed named on.exit expression so you could
remove particular on.exit expressions. However, now I think I can almost always
avoid such considerations and make code clearer by making a new function call,
often involving a lazily evaluated expression, whenever I need a new on.exit
expression. E.g., instead of
f0 <- function(x, y) {
oldWarn <- options(warn=0)
on.exit(oldWarn)
oldMar <- par(rep(2,4))
on.exit(par(oldMar))
plot(log(x), y)
}
use
f1 <- function(x, y) {
suppressWarnings(
withPar(list(mar=rep(2,4)),
plot(log(x), y)
)
)
}
suppressWarnings already exists and withPar could be
withPar <- function(parList, expr) {
oldPars <- par(parList)
on.exit(par(oldPars))
expr
}
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-----Original Message-----
From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf
Of Henrik Bengtsson
Sent: Sunday, November 03, 2013 1:42 PM
To: R-devel
Subject: [Rd] WISHLIST: on.exit(..., add=TRUE, where="first") to address common use
cases
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
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel