Using R from Java using a NetBeans Project

R is an open source statistical package. It is basically a free/open source competitor to SAS. R is both a language and an environment – you write scripts in the R language to run your analysis. R has a huge ecosystem of packages (CRAN) which can be easily installed to perform more complex analyses. It is used by and designed for statisticians.

Since the the subject of this post is Java + R, why use R and Java together? Well, Java is an excellent general purpose programming language. However, although you can definitely implement statistical tests using Java, that is a lot of work. Also, statisticians use R – Java is too primitive for them. It is easy to write Java code to calculate an average/standard deviation/etc. however it is another thing to implement the algorithms for modeling using bayesian networks. Doing hardcore math with Java requires understanding floating point math in addition to statistics (not something in abundance with the generic software developer). Unless you are trying to implement a statistics library from scratch then you should use R.

R can be easily accessed from Java. This means that you can have your statistician write an R script to perform whatever analysis you want, and then invoke it from Java. To do so, you use the rJava package.

If you are completely new to R, you will need to do the following:

After installing R, you will need to install the rJava package.

  • Launch RStudio or ‘R’ from the command line.
  • type ‘install.packages(“rJava”)’ and hit return – this will install the Java packages and JNI libraries necessary to use R from Java.

Now that you have R and rJava setup, it is time to create a Java program to use R to perform some statistics. To do so, you will need to know where R is installed so you can fine the library dependencies (JARs/JNI libraries etc.). The Java program will also need to know the location of “R Home”.

In either R or RStudio type ‘R.home()’ and hit return, the path to R will then be printed out. On my Mac, it returns:

[1] “/Library/Frameworks/R.framework/Resources”

Before we fire up NetBeans, we need to edit the netbeans.conf file. On the Mac, netbeans.conf is located here:

/Applications/NetBeans/NetBeans 8.0.1.app/Contents/Resources/NetBeans/etc/netbeans.conf

Add an export to the file for R home – with my installation of R, I added the following to the netbeans.conf file (end of the file):

export R_HOME=/Library/Frameworks/R.framework/Resources

After this is done, it is time to fire-up NetBeans and create a new Java project (New Project | Java Application).

Once you have a new project created, it is time to add the R JAR and JNI libraries. Open Project Properties using File | Project Properties. Add the dependencies as in the following screenshot:

R_ProjectProperties

You use the Add Jar/Folder button to add the dependencies. The folder which is added (last entry) contains the JNI library. On the Mac, the folder contains libjri.jnilib. If the directory is incorrect or not added, you will get an error message that the native library could not be loaded.

Now that we have a project, how do we go about using R? The following two code files will get you up and running:

  • TestR.java – contains a main method, initializes R, and performs a simple ‘mean’ calculation of the numbers 1-6.
  • TestConsole.java – implements a bunch of callbacks – required for the code in TestR.

TestR.java

import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;

public class TestR {
 
 public static void main(String args[]) {
 if (!Rengine.versionCheck()) {
 System.err.println("Java version mismatch.");
 System.exit(1);
 }
 String my[] = { "--vanilla" };
 Rengine re=new Rengine(my,false,new TextConsole());
 if (!re.waitForR()) {
 System.out.println("Cannot load R");
 System.exit(1);
 }
 REXP result = re.eval("mean(1:6)");
 System.out.println("rexp: " + result.asDouble());
 }
}

TextConsole.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.rosuda.JRI.RMainLoopCallbacks;
import org.rosuda.JRI.Rengine;

public class TextConsole implements RMainLoopCallbacks {

 @Override
 public void rWriteConsole(Rengine re, String text, int oType) {
 System.out.print(text);
 }

 @Override
 public void rBusy(Rengine re, int which) {
 System.out.println("rBusy(" + which + ")");
 }

 @Override
 public String rReadConsole(Rengine re, String prompt, int addToHistory) {
 System.out.print(prompt);
 try {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String s = br.readLine();
 return (s == null || s.length() == 0) ? s : s + "\n";
 } catch (Exception e) {
 System.out.println("jriReadConsole exception: " + e.getMessage());
 }
 return null;
 }
 
 @Override
 public void rShowMessage(Rengine re, String message) {
 System.out.println("rShowMessage \"" + message + "\"");
 }

 @Override
 public String rChooseFile(Rengine re, int newFile) {
 return "";
 }

 public void rFlushConsole(Rengine re) {
 }

 public void rLoadHistory(Rengine re, String filename) {
 }

 public void rSaveHistory(Rengine re, String filename) {
 }
}

You are up and running with Java and R!

Excellent Meeting at Connecticut Java User Group on Ceylon

Gavin King and Stephan Epardaud gave an excellent introduction to Ceylon at the October Connecticut Java User Group meeting. This is one of the most interesting new languages I have seen in a while – it compiles down to both Java byte code and also JavaScript. Thus you can write common APIs that would run in both the JVM and within the browser. Given the current emphasis on SPA (Single Page Applications)  this could really made development easier – if I can use the same objects in both the browser and on the server.

Ceylon looks to be a significant leap forward – Gavin did an excellent job of contrasting Ceylon with Java and explaining how it was different/better. I would prefer it over JavaScript any day of the week. Ceylon also looks more approachable than Scala. I’ve been trying to learn Scala on the weekends (among other things) but the learning curve is substantial – especially since I am not actively using it during the week. I have doubts that many 9-5 developers could successfully transition to it. Ceylon’s trump feature in my mind is the ability to run it in a JavaScript engine – that makes it easier to promote its use at work. There is clear value in the language – I can define logic and behavior and share them between the browser and the backend server application.

There was much to digest in the presentation. Ceylon has its own module system (focus of a lot of animated discussion during the meeting) and hence its own build system. Although there is support for Ant, path forward with Maven/Gradle wasn’t too clear – Maven support was mentioned but I wasn’t sure whether I could be successful today. The tooling support centers on Eclipse (plug-in) although there is a plugin for IntelliJ. Tooling of course will change over time – the support in Eclipse looks really good. With the help of the IDE it should be much easier to learn Ceylon and improve code written in Ceylon.

The biggest thing needed at this point for Ceylon is an introductory book!

The JUG meeting was well attended  and basically everyone stayed until almost 9:30 – long past when the meeting was supposed to end. Pre-registered we had 24 – still not up to 45-50 which we used to see 10 years ago. Meeting have been trending upwards this year (as well as membership).

IMG_3638