Skip to content

Fetching Warning Messages

4 messages · Nikhil Shah, Brian Ripley, Henrik Bengtsson

#
Hi,

    I am facing one problem of fetching R warning messages in Java Code
using Rserve. It is easier to trap R Error messages by using catching
RSrvException. I came to know one way of fetching R Warning messages, i.e.
using "withCallingHandlers", below is my Java Program, which uses
withCallingHandlers of R :
import org.rosuda.JRclient.*;

---------------------------RWarning.java-----------------------
class RWarning
{
   public static void main(String args[])
  {
     try
     {
       String hostName = null;
       hostName = args[0];
       Rconnection c = new Rconnection(hostName);
       c.voidEval("lastWarning <- NULL");
       c.voidEval("withCallingHandlers(
{x<-sqrt(-9);y<-matrix(1:9,ncol=4);z<-sqrt(4)} , warning = function (w) {
lastWarning <<- paste(lastWarning,as.character(w))})"); //This will generate
warning message[sqrt(-9)], another warning message [ matrix(1:9,ncol=4) ]
and successful completion [ sqrt(4) ]
        System.out.println(c.eval("z").asDouble());
        System.out.println(c.eval("lastWarning").asString());
        c.close();
        System.out.println("DONE");
      }
      catch(RSrvException  e)
      {
         System.out.println("Error : " + e.getMessage());
         e.printStackTrace();
      }
   }
}
---------------------------End Of RWarning.java-----------------------

Output of above program is (as expected) :

2.0
simpleWarning in sqrt(-9): NaNs produced
simpleWarning: data length [9] is not a sub-multiple or multiple of the
number of columns [4] in matrix

DONE



     Now my query is that if there is any way of using warnings() function
in Java Program to fetch all warnings. I used it in my program but returns
me NULL instead of warning messages. I also used last.warning but it Java
Program gives an error saying that last.warning object is not found. I have
pasted both the java code below :

This is the java program that I have written to use "last.warning" object of
R. Please explain me where the error could be.

------------Code of RWarning1.java----------------

import org.rosuda.JRclient.*;

class RWarning1
{
  public static void main(String args[])
  {
    try
    {
      String hostName = null;
      hostName = args[0];
      Rconnection c = new Rconnection(hostName);
      System.out.println(c.eval("x<-sqrt(-9)").asString());
      System.out.println(c.eval("last.warning").asString());
      c.close();
      System.out.println("DONE");
    }
    catch(RSrvException  e)
    {
      System.out.println("Error : " + e.getMessage());
      e.printStackTrace();
    }
  }
}
------------End of code of RWarning1.java------------------

output of RWarning1.class

null
Error : Request return code: 127 [request status: Error (127)]
org.rosuda.JRclient.RSrvException: Request return code: 127 [request status:
Err
or (127)]
        at org.rosuda.JRclient.Rconnection.eval(Rconnection.java:190)
        at RWarning.main(RWarning.java:13)

In other words, when I use "last.warning" in eval method, I simply get an
exception, instead of value of last.warning.


Below is the java code of using warnings() function.

------------Code of RWarning2.java----------------

import org.rosuda.JRclient.*;

class RWarning2
{
  public static void main(String args[])
  {
  try
  {
    String hostName = null;
    hostName = args[0];
    Rconnection c = new Rconnection(hostName);
    System.out.println(c.eval("x<-sqrt(-9)").asString());

System.out.println(c.eval("paste(capture.output(warnings()),collapse='\n')")
.asString());
    c.close();
    System.out.println("DONE");
  }
  catch(RSrvException  e)
  {
    System.out.println("Error : " + e.getMessage());
    e.printStackTrace();
  }
}
}
------------End of code of RWarning2.java------------------
output of RWarning2.class

null
NULL
DONE


  Please let me know where I am making mistake.

Regards,

Nikhil Shah
#
Please read the help page on options("warn") and see what warnings() 
does. (I am pretty sure you have asked this and been told before.)

There should not be an object called last.warning in your example.
On Mon, 22 Aug 2005, Nikhil Shah wrote:

            

  
    
#
Hi,

    I read the help page and saw that warnings() actually prints the warning
message and stores warning in top-level variable "last.warning". So it seems
that it is almost impossible to access last warning from java code. I got
another solution of fetching warning messages by storing warning messages in
a file and reading the file later. This can be done by using sink() command,
but before that options(warn=1) must be set.
I read help page
i.e.
{
generate
matrix(1:9,ncol=4) ]
function
returns
Java
have
object of
status:
an
System.out.println(c.eval("paste(capture.output(warnings()),collapse='\n')")
Regards,

Nikhil Shah
#
Did you get my reply to you on Aug 13 on withCallingHandlers()?

  https://stat.ethz.ch/pipermail/r-devel/2005-August/034189.html

/Henrik
Nikhil Shah wrote: