On 09/11/2012 04:19 PM, Henrik Bengtsson wrote:
Hi,

I'm trying to implement an abort() method that works just like stop()
but does not signal the condition such that try() and tryCatch(...,
condition=...) are, contrary to stop(), effectively non-working with
abort() calls.

In order to achieve this, I stumbled upon invokeRestart("abort"), cf.
help("invokeRestart", package="base") that reads "Restarts are used
for establishing recovery protocols. They can be established using
withRestarts. One pre-established restart is an abort restart that
represents a jump to top level.".

So, my current implementation is (roughly):

abort <- function(...) {
  # handling messages etc

  # Fully abort the R evaluation and return to the top level
  invokeRestart("abort")
}

I've tested it in various setups with and without tryCatch(...,
condition=...) and so on and it appears to work.  Does anyone know if
I'm overlooking something or can I count on  invokeRestart("abort") to
always stop any currently evaluated R code?

Not sure what 'currently evaluating R code' means, but

  f = function(x) {
      on.exit(cat("not dead yet\n"))
      invokeRestart("abort")
  }

> f()
never say die

  g = function() {
      reg.finalizer(new.env(), function(...)
          cat("not dead yet\n"))
      invokeRestart("abort")
  }
> g()
> gc()
not dead yet
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 170841  9.2   47185920 2520   709729 38.0
Vcells 145992  1.2  268435456 2048  1023614  7.9

  h = function() {
      withRestarts(f(), abort=function(...) {
          cat("I'm sorry Henrik, I can't do that\n")
          TRUE
       })
  }

> h()
never say die
I'm sorry Henrik, I can't do that
[1] TRUE

all evaluate code after invoking abort.


Also, does anyone know how far back (in R versions) invokeRestart("abort") goes?

$ svn blame conditions.Rd

says that the line you quote is from r25527 (which is when tryCatch appears to have been introduced), and

$ svn info -r25527
Path: man
URL: https://svn.r-project.org/R/trunk/src/library/base/man
Repository Root: https://svn.r-project.org/R
Repository UUID: 00db46b3-68df-0310-9c12-caf00c1e9a41
Revision: 25527
Node Kind: directory
Last Changed Author: luke
Last Changed Rev: 25527
Last Changed Date: 2003-07-31 12:35:18 -0700 (Thu, 31 Jul 2003)


Thxs,

Henrik

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



--
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to