[Rd] Handling warning messages

Henrik Bengtsson hb at maths.lth.se
Sat Aug 13 12:39:38 CEST 2005


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 at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
>



More information about the R-devel mailing list