Re: [Rd] Handling warning messages

2005-08-13 Thread Henrik Bengtsson
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.  Iexample:

foo <- function(...) {
   print("1");
   warning("A warning!");
   print("2");
   warning("Another warning!");
   print("3");
}

 > options(warn=0)
 > foo()
[1] "1"
[1] "2"
[1] "3"
Warning messages:
1: A warning! in: foo()
2: Another warning! in: foo()

 > options(warn=1)
 > foo()
[1] "1"
Warning in foo() : A warning!
[1] "2"
Warning in foo() : Another warning!
[1] "3"

 > options(warn=2)
 > foo()
[1] "1"
Error in foo() : (converted from warning) A warning!

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:

 > options(warn=0)
 > tryCatch(foo(), finally={cat("done!\n")})
[1] "1"
[1] "2"
[1] "3"
done!
Warning messages:
1: A warning! in: foo()
2: Another warning! in: foo()
 > tryCatch(foo(), warning=function(w) str(w), finally={cat("done!\n")})
[1] "1"
List of 2
  $ message: chr "A warning!"
  $ call   : language foo()
  - attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition"
done!

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:

 > options(warn=0)
 > withCallingHandlers(foo(), warning=function(w) str(w))
[1] "1"
List of 2
  $ message: chr "A warning!"
  $ call   : language foo()
  - attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition"
[1] "2"
List of 2
  $ message: chr "Another warning!"
  $ call   : language foo()
  - attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition"
[1] "3"
Warning messages:
1: A warning! in: foo()
2: Another warning! in: foo()

To avoid the list warnings at the end when the top-level function 
finished, try

 > options(warn=-1)
 > withCallingHandlers(foo(), warning=function(w) str(w))
[1] "1"
List of 2
  $ message: chr "A warning!"
  $ call   : language foo()
  - attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition"
[1] "2"
List of 2
  $ message: chr "Another warning!"
  $ call   : language foo()
  - attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition"
[1] "3"


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.

Best wishes

Henrik Bengtsson


> And Java programming questions are inappropriate for the
> R-help mailing list.
> 
> --
> SIGSIG -- signature too long (core dumped)
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Handling warning messages

2005-08-13 Thread Paul Roebuck
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)

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel