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
warning.expression?
8 messages · Bert Gunter, Roger D. Peng, Thomas Friedrichsmeier +3 more
?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
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Thomas
Friedrichsmeier
Sent: Thursday, September 22, 2005 9:14 AM
To: r-help at stat.math.ethz.ch
Subject: [R] warning.expression?
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
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
You might be interested in 'tryCatch' to catch warnings. -roger
Thomas Friedrichsmeier wrote:
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
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/
?warnings
No, sorry, this does not work. Try this, if you like:
myfunc <- function () {
warnings ()
}
options (warning.expression=quote (myfunc ()))
warning ("test")
Also see ?conditions
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?
Note that both were suggested by help.search('warning') -- Please do make
full use of R's internal documentation before posting.
It's quite possible, I'm overlooking something rather obvious. Not that obvious, however. Regards Thomas
You might be interested in 'tryCatch' to catch warnings.
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:
You might be interested in 'tryCatch' to catch warnings.
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?
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?
Thomas
Randall Schulz
Thomas Friedrichsmeier wrote:
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?
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:
You might be interested in 'tryCatch' to catch warnings.
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
-roger Thomas Friedrichsmeier wrote:
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
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html