[Rd] top level condition handlers

Romain Francois romain.francois at dbmail.com
Tue Apr 14 17:32:24 CEST 2009


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



More information about the R-devel mailing list