From romain.francois at dbmail.com  Tue Apr 14 17:32:24 2009
From: romain.francois at dbmail.com (Romain Francois)
Date: Tue, 14 Apr 2009 17:32:24 +0200
Subject: [Rd] top level condition handlers
Message-ID: <49E4AC88.2040900@dbmail.com>

Hello,

I would like to establish top level condition handlers and restarts, so 
that I don't explicit calls to withCallingHandlers and withRestarts in 
many places:

For example :

 > customError
function( message ){
  err <- simpleError( message )
  class( err ) <- c( "customError", class( err) )
   err
}
 > withCallingHandlers( { signalCondition(customError( "ouch")) } , 
customError = function(e) cat( "gotcha : ", e$message, "\n" ) )
gotcha :  ouch
NULL

I'd like to be able to do something like this:

 > topLevelCallingHandlers( customError = function(e) cat( "gotcha : ", 
e$message, "\n" ) )
 > signalCondition( customError( "ouch") )

and the "customError" condition to be caught by the handler I set up 
previously.

I tried modifying withCallingHandlers like this:

topLevelCallingHandlers <- function(...) {
    handlers <- list(...)
    classes <- names(handlers)
    if (length(classes) != length(handlers))
        stop("bad handler specification")
    .Internal(.addCondHands(classes, handlers, .GlobalEnv, .GlobalEnv, 
TRUE))
    invisible( NULL )
}
 > withCallingHandlers
function (expr, ...)
{
    handlers <- list(...)
    classes <- names(handlers)
    parentenv <- parent.frame()
    if (length(classes) != length(handlers))
        stop("bad handler specification")
    .Internal(.addCondHands(classes, handlers, parentenv, NULL,
        TRUE))
    expr
}
<environment: namespace:base>

but it does not work, probably because the handler stack is reset 
somewhere.

Would it work if I poke into the RTopLevel.handlerstack instead of the 
R_HandlerStack as .addCondHands is doing ?

R_Toplevel.handlerstack = R_HandlerStack;
R_Toplevel.restartstack = R_RestartStack;
R_GlobalContext = R_ToplevelContext = &R_Toplevel;

Romain

-- 
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr



[Rd] top level condition handlers
Romain Francois