From hb at biostat.ucsf.edu  Wed Sep 12 19:08:42 2012
From: hb at biostat.ucsf.edu (Henrik Bengtsson)
Date: Wed, 12 Sep 2012 10:08:42 -0700
Subject: [Rd] Is invokeRestart("abort") unstoppable?
In-Reply-To: <504FFFF0.4080602@fhcrc.org>
References: <CAFDcVCQCEUGuWcD2nqpzAxMwsao=QT=nkhzG5OZop5hostjsKw@mail.gmail.com>
	<504FFFF0.4080602@fhcrc.org>
Message-ID: <CAFDcVCRquP1BvmO87TadEVxZx=pZGTwrqa=2i0zoGVOzJC=88w@mail.gmail.com>

Thanks for you help Martin,

On Tue, Sep 11, 2012 at 8:22 PM, Martin Morgan <mtmorgan at fhcrc.org> wrote:
> 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("never say die\n"))
>       invokeRestart("abort")
>   }
>
>> f()
> never say die

I forgot about on.exit(), though this is how I'd like abort() to
behave, i.e. abort() = stop() minus signalling.

>
>   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

This one too.

>
>   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.

This one I would never figure out/think of myself.  Although the abort
is caught here, at least the behavior of abort() is consistent with
stop() here too.  So for now, I'm (still) satisfied.

>
>
>>
>> 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)

Perfect.

/Henrik

>
>>
>> Thxs,
>>
>> Henrik
>>
>> ______________________________________________
>> R-devel at 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



[Rd] Is invokeRestart("abort") unstoppable?
Henrik Bengtsson