Skip to content

warning.expression?

8 messages · Bert Gunter, Roger D. Peng, Thomas Friedrichsmeier +3 more

#
Hi!

I'm trying to catch all warning-messages for special handling. It seems 
options (warning.expression=myfunc ()) can be used for that. However, the 
question is: How can I get at the actual warning in myfunc ()? Apparently in 
S, you can use .C("get_last_message") for that. Is there a similar mechanism 
in R?

Thanks for your help!
Thomas
#
?warnings

Also see ?conditions

Note that both were suggested by help.search('warning') -- Please do make
full use of R's internal documentation before posting.

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box
#
You might be interested in 'tryCatch' to catch warnings.

-roger
Thomas Friedrichsmeier wrote:

  
    
#
No, sorry, this does not work. Try this, if you like:

myfunc <- function () {
	warnings ()
}
options (warning.expression=quote (myfunc ()))
warning ("test")
tryCatch ()
and
withCallingHandlers ()

are a whole different story, as far as I understand them. I actually want 
_all_ warnings in the whole session to go through my handler (actually I'm 
writing a GUI front-end, and would like to have a way to easily identify all 
warnings).

That's what I thought warning.expression was designed for. So am I wrong in 
thinking so? If not, how to use it correctly?
It's quite possible, I'm overlooking something rather obvious. Not that 
obvious, however.

Regards
Thomas
#
Yes, thanks for pointing it out. However, I'm actually looking for a way to 
catch all warnings in a whole (interactive) session. Can warning.expression 
be used for that?

Thomas
#
Thomas,
On Thursday 22 September 2005 10:00, Thomas Friedrichsmeier wrote:
Please permit, if you will, some feedback from someone who is quite 
uneducated in R, at least so far, but who has a lot of general 
programming background.

I'm afraid this does not make sense to me. An "interactive session" is 
not some monolithic blob. It is a sequence of one or more (often very 
many) discreet interactions. Any one of these may produce an 
exceptional condition, and it seems to me that each individual 
interaction that elicits an exception should be handled independently 
and if it does produce an exceptional condition, that should be 
reported as soon as it occurs. Things should then be cleaned up, as 
necessary and feasible, and the interactive session should be allowed 
to continue.

Does this make sense? Or am I misunderstanding your use case?
Randall Schulz
#
Thomas Friedrichsmeier wrote:

            
I've just been nosing around the source code in errors.c, and it 
doesn't look good.

In this function:

static void vwarningcall_dflt(SEXP call, const char *format, va_list ap)

the warning.expression 's' is called here:

        cptr = R_GlobalContext;
         while ( !(cptr->callflag & CTXT_FUNCTION) && cptr->callflag )
             cptr = cptr->nextcontext;
         eval(s, cptr->cloenv);
	return;

but when the expression is null/nil the code goes on to the default 
case, in which it gets the warning message from the 'call' parameter:


  dcall = CHAR(STRING_ELT(deparse1(call, 0, SIMPLEDEPARSE), 0));
  REprintf(_("Warning in %s : "), dcall);

  So I don't see how this parameter can be available to the 
warning.expression call. There may be a way, but I don't see it.

  It seems a bit dumb that warning.expression functions can only say 
"Hey, something a bit iffy may have ocurred, but I dont know what and I 
dont know where!". Maybe there's something in that cptr->cloenv that can 
tell you...

  Otherwise it requires patching.

Baz
#
Roger D. Peng wrote:
A warning here: tryCatch(expr, warning=...) will catch warnings and 
interrupt 'expr' (as if an error occured), whereas 
withCallingHandlers(expr, warning=...) does not do this.

/Henrik