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

[code language=”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());
}
}
[/code]

TextConsole.java

[code language=”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) {
}
}
[/code]

You are up and running with Java and R!

5 thoughts on “Using R from Java using a NetBeans Project”

  1. Pingback: Plotting from R into a Java JDialog with JavaGD | Kabasumo

  2. Pingback: Make bridge between R and Java work | Kabasumo

Comments are closed.

%d bloggers like this: