Tuesday, July 12, 2011

Debugging the R output of RCaller

RCaller 2.0 has been submitted to Google Code before two or three days. Many RCaller users are testing it and except a Windows installation it seems not to be so problematic.

RCaller 2.0 receives the R outputs as XML files. If the user does not know the returned variable names or if there was a problem with the results some debugging stuff is needed.

Now the function 'getXMLFileAsString()' is implemented in RCaller, by using it, the converted R output can be investigated.

Suppose that we want to run some R code from Java and we want to have a look at the returned XML content. Have a look at these codes:



package test;

import rcaller.RCaller;

/**
 *
 * @author Mehmet Hakan Satman
 */

public class Test4 {
   
    public static void main (String[] args){
        new Test4();
    }
   
    public Test4(){
        try{
            /*
             * Creating an instance of RCaller
             */
            RCaller caller = new RCaller();
           
            /*
             * Defining the Rscript executable
             */
            caller.setRscriptExecutable("/usr/bin/Rscript");
           
            /*
             * Some R Stuff
             */
            caller.addRCode("set.seed(123)");
            caller.addRCode("x<-rnorm(10)");
            caller.addRCode("y<-rnorm(10)");
            caller.addRCode("ols<-lm(y~x)");
           
            /*
             * We want to handle the object 'ols'
             */
            caller.runAndReturnResult("ols");
           
            /*
             * Getting R results as XML
             * for debugging issues.
             */
            System.out.println(caller.getParser().getXMLFileAsString());
        }catch(Exception e){
            System.out.println(e.toString());
        }
    }
}



Because of the "set.seed()"  function of R, this code should produce the same results for all machines. The XML output is






This structure of this XML file is simple and one can see that each single variable is encapsulated within a "" and a "" tag.

Another way of handling the variable names is to use "ROutputParser.getNames()". This function simply returns an ArrayList which includes the variable names returned by R.

No comments:

Post a Comment

Thanks