From roebuck at odin.mdacc.tmc.edu  Sat Aug 13 20:14:54 2005
From: roebuck at odin.mdacc.tmc.edu (Paul Roebuck)
Date: Sat, 13 Aug 2005 13:14:54 -0500 (CDT)
Subject: [Rd] Handling warning messages
In-Reply-To: <42FDCDEA.50602@maths.lth.se>
References: <004801c59f28$e1147650$7900a8c0@dca.com>
	<Pine.OSF.4.58.0508120848130.268788@odin.mdacc.tmc.edu>
	<42FDCDEA.50602@maths.lth.se>
Message-ID: <Pine.OSF.4.58.0508131204470.336310@odin.mdacc.tmc.edu>

On Sat, 13 Aug 2005, Henrik Bengtsson wrote:

> Paul Roebuck wrote:
>
> > On Fri, 12 Aug 2005, Nikhil Shah wrote:
> >
> >>I have query regarding R & Rserve. In Rserve, there is a
> >>way to capture Errors by RSrvException class, but is there
> >>any way to capture warning messages?
> >
> > options(warn = 2) work for you?
>
> Be careful. This will turn warnings into errors (as ?options
> says) and which in turn will interrupt your code.
> [SNIP examples]
>
> Is this really what you want when you say "capture warning
> messages"? From your code I assume you would to get a list
> of warning messages after the code is done?!?
>
> >>I have found that there is "warnings()" command in R, which
> >>lists the last warning message, but I am not able to get
> >>the warning message in java program by executing the
> >>following line:
> >>
> >>REXP rx = null;
> >>// will generate warning message
> >>rx = connection.eval("x<-sqrt(-9)");
> >>// this displays null instead of warning message
> >>connection.eval("warnings()").asString();
> >>
> >>Please reply me correct way, if any, to display warning
> >>message.
> >
> > Probably need some mods to your Java source to handle this
> > but something like the following would enable you to
> > receive notice of errors/warnings. But it's hard to answer
> > this question in terms of context since I have no idea what
> > you're doing.
> >
> > tryCatch(x<-sqrt(9),
> >          warning = function(w) w,
> >          error = function(e) e)
>
> A note of concern is needed here: this will interrupt the
> expression evaluated as soon as a warning occurs!  Example:
> [SNIP example]
>
> I haven't investigated it in details, but maybe
> withCallingHandlers() is what you could use. This will
> "detect" (not "capture" in the above sense) warnings and
> other conditions when they occur and call the specified
> function.  See ?conditions for details. Example:
> [SNIP example]
>
> To avoid the list warnings at the end when the top-level
> function finished, try
> [SNIP example]

Henrik has provided a much more in-depth answer. Nice job
on the examples there. Mine was obviously quick'n'dirty.
There was not enough context in the original question to
say what would be good enough.

If Nikhil is using the R environment as a black box function
call, simply handling warnings as errors might be appropriate.
Often in that case, one is only interested in the end result
and only want to know if anything could be wrong. To do
step-by-step monitoring of a complex script, one could also
run R as a pseudoterm'd coprocess and parse the stream
output from Java code. I used this approach on a couple
projects several years ago and it worked quite well.

> I have tried to ellaborate with the top-level variable
> 'last.warning' (see under 'warn' in ?options), but I
> never succeeded; it seems to be set first when the
> top-level function finishes.

I was originally going to suggest that as well but also
had problems retrieving that value.

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)



[Rd] Handling warning messages
Paul Roebuck