[Rd] restart

Luke Tierney luke@stat.uiowa.edu
Tue, 19 Nov 2002 21:39:32 -0600 (CST)


On Wed, 20 Nov 2002 Mark.Bravington@csiro.au wrote:

> Dear group
> 
> I use "restart" in part of my code, in a way that's not easily changed to
> "try". As I convert code from R1.5.0 to R1.6.1, I'm getting ugly messages;
> the help system says to contact r-devel, so here I am. This one's a bit
> complicated-- sorry!
> 
> The context is inside a debugger (I have an R and S debugger that offers
> stand-alone code windows, line numbered conditional breakpoints,
> statement-skipping, and graceful error trapping). I use the following
> function to trap errors, both in code undergoing debugging, and in the
> user's interactions with the debugger:
> 
> eval.catching.errors <- function(i, envir) do.in.envir( envir=find.debug.HQ(
> FALSE), {
>   if(.evaluated.OK.) {
>     .evaluated.OK. <<- FALSE
>     restart(TRUE)
>     j <- eval( i, envir=envir)
>     .evaluated.OK. <<- TRUE
> return(j) }
>   else
> return(NULL)
> })
> 
> The function is a bit opaque because I have fiddled with the lexical
> scoping, but the rationale is fairly simple. There is a 'global flag' called
> ".evaluated.OK." which is normally set to TRUE. When "eval.catching.errors"
> is called, the flag is reset to FALSE and there's an attempt to evaluate the
> expression given in "i". If there are no errors in evaluation, then the
> global flag is reset to TRUE before "eval.catching.errors" exits. Otherwise,
> "eval.catching.errors" is reinvoked (thanks to "restart" catching the error)
> and exits with the global flag set to FALSE. Subsequent debugger code
> inspects the status of the global flag to decide what happens next.
> 
> The problem with changing 
> 
>   restart( TRUE)
>   j <- eval( i, envir=envir)
> 
> into
> 
>   j <- try( eval( i, envir=envir))
>   if( inherits( j, 'try-error')) <<...>>
> 
> is that the statement being debugged may itself be a call to "try" which
> happens to fail. In that case, the debugger would stop and report an error,
> whereas the code-being-debugged has actually been designed to run smoothly
> in the event of error. Under R1.5.0, errors that occur within a "try"
> statement in the code-being-debugged are (correctly) not flagged as problems
> by my debugger.
> 

One way to handle this is to wrap the result, something like

   j <- try( list(value = eval( i, envir=envir)))
   if( inherits( j, 'try-error')) <<...>>
   ...
   return(j$value)

luke

-- 
Luke Tierney
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
   Actuarial Science
241 Schaeffer Hall                  email:      luke@stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu