[Rd] Suggestion: Add simpleExit condition

Henrik Bengtsson hb at stat.berkeley.edu
Mon Aug 27 20:22:48 CEST 2007


Hi,

I would like to bring up and old suggestion
[http://tolstoy.newcastle.edu.au/R/devel/06/03/4512.html] of adding an
exit condition, so that for instance it is possible to silently exit
from scripts and Rd examples similar to return() for functions, e.g.

\examples{
require("foo") || exit("Example will not run without the 'foo' package.")
...
}

I know this can be solved by a simple if {require("foo")} { }
statement, but the above is to avoid such nested code, especially in
example code, cf. return() and break.

Here are the details to get this working:

1. Define a new condition class similar to simpleWarning() and simpleError():

simpleExit <- function(...) {
  cond <- simpleCondition(...)
  class(cond) <- c("simpleExit", class(cond))   cond
}

2. Setup a method to generate such a condition similar to warning() and stop():

exit <- function(...) {
  invisible(signalCondition(simpleExit(...))) }
}

3. That is the basic framework. We can then use tryCatch() to catch
and return upon a simpleExit as follows:

evalWithExit <- function(...) {
  tryCatch(..., simpleExit=function(cond) cond) }
}

Then it is matter of flavor if one wants to update source() with an
argument 'allowExits=FALSE' or have a standalone function:

sourceWithExit <- function(...) {
  evalWithExit(source(...))
}

5. The code example() needs to be updated accordingly, i.e. replacing
source() with sourceWithExit().

6. src/scripts/check.in needs to updated so that the examples are
sourced detecting simpleExit:s.

Steps 1-4 can be added without side effects.  It is Step 5 & 6 that
needs extra care.  I am happy to contribute the above if it would be
accepted.

Best,

Henrik

PS. As I queried on in my follow up of the March 2006 thread, if there
is a way to signal user-interrupts programatically, all of the above
is not necessary. DS.



More information about the R-devel mailing list