[Rd] Fetching Warning Messages

From: Nikhil Shah <n.shah_at_decisioncraft.com>
Date: Mon 22 Aug 2005 - 15:26:59 GMT


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



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue Aug 23 01:27:28 2005

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:16 GMT