RCaller Home

Change Log for version 0.5.2:

  • Added a multi-threaded StreamReader class to RCaller, for stream reading both stderr & stdout to prevent read blocks.
  • StreamReader will optionally echo what it receives to the parent process stdout & stderr, so you can see what is going on
  • Changed RunRCode to use StreamReader
  • Changed RunRCode to wait for the sub-process to complete before returning
  • int[] RGetAsIntArray(String name) function was added so results from R functions can be handled as integer arrays
  • String[] RGetAsStringArray(String name) function was added in order to handle R results as String arrays
  • Removed extra cat(javaCode) call from makejava.r


[2010/08/07] Now, Rcaller has a new version, 0.5.2, with some bug fixes and additional functionality. Some changes are done and some bugs are fixed by John Oliver. John is now second developer of the Rcaller.

Examples

1)Getting Pi from R!


In this example, we are calling R code "a<-pi;" that sets the value of pi to variable a. Then, we handle this result from Java.

	RCaller caller=new RCaller();
        StringBuffer code=new StringBuffer();
        code.append("a<-pi;cat(makejava(a))");
        try{
            caller.RunRCode(code.toString(),false,false);
            System.out.println(caller.RGet("a[0]"));
        }catch (Exception e){
            System.out.println(e);
        }

The result is 3.14159. RCaller always handles results as arrays, so a is not variable but double array. Array has only one element, so a[0] is the value that sent from R. We have to use cat(makejava(a)) to make R object 'a' usable in Java.
We call RunRCode() function with 3 parameters. Last 2 parameters are boolean. If first one is true, then content of stderr will be written on console. If the second one is true, then content of stdout will be written. We set them false not to write both outputs on the screen.

2)Calculate Linear Regression from Java using R


In this example, we set x and y with random variables that come from standard normal distributions and estimate linear regression using R and Java.

	RCaller caller=new RCaller();
        StringBuffer code=new StringBuffer();
        code.append("x<-rnorm(10);");
        code.append("y<-rnorm(10);");
        code.append("ols<-lm(y~x);");
        code.append("cat(makejava(ols));");
        try{
            caller.RunRCode(code.toString(),false,false);
            double[] coefs=caller.RGetAsDoubleArray("coefficients");
            for (int i=0;i<coefs.length;i++) System.out.println(coefs[i]);
        }catch (Exception e){
            System.out.println(e);
        }

The result is
-0.815634476060036
0.637334790434423

so, these are the estimated coefficients of the ordinary least squres regression.

3)Running RCaller in different platforms (Linux, Windows, Mac, etc)


RCaller is pure Java and can be run any platform that Java virtual machine runs. Also, you need to be have R as well. Default R engine is Rscript executable file that distrubited in R. Default value of engine is /usr/bin/Rscript but user can change location using setRScriptExecutableFile(String location) method.

	RCaller caller=new RCaller();
        caller.setRScriptExecutableFile("C:\\Program Files\\...\\R\\..\\Rscript.exe");
	//caller.setRScriptExecutableFile("/usr/bin/Rscript");

4)What objects returned after running my R command?


RCaller converts R objects to Java objects. You can handle returned values' names like this:

	RCaller caller=new RCaller();
        StringBuffer code=new StringBuffer();
        code.append("x<-rnorm(10);");
        code.append("y<-rnorm(10);");
        code.append("ols<-lm(y~x);");
        code.append("s<-summary(ols);");
        code.append("cat(makejava(s));");
        try{
            caller.RunRCode(code.toString(),false,false);
            ArrayList fields=caller.getFieldList();
            for (int i=0;i<fields.size();i++) System.out.println(fields.get(i));
        }catch (Exception e){
            System.out.println(e);
        }

The result is:
double[] residuals
double[] coefficients
double[] sigma
double[] df
double[] rsquared
double[] adjrsquared
double[] fstatistic
double[] covunscaled
double[] residuals
double[] coefficients
double[] sigma
double[] df
double[] rsquared
double[] adjrsquared
double[] fstatistic
double[] covunscaled

and these are all returned fields from the summary() R command.