How to Integrate R with Java?

Java is undisputedly a great language for building enterprise solutions, but has miles to scale on the analytics front. Of course, there are effective tools like Weka, whose algorithms could be called from Java codes. Weka is empowered with tools for data pre-processing, classification, regression, clustering, association rules, and visualization. It is also well-suited for developing new machine learning schemes.
On that note, this blog explores the opportunities of integrating Java with the R language, which is widely used among statisticians and data miners for developing statistical software and data analysis. R is enriched with Machine Learning and Statistical Libraries.
Integrating R with Java could create some real-time, high end Machine Learning based applications. This blog touches upon the benefits and architectures where such kind of integration may be required.
There are three possible ways to connect R and Java. To integrate R and Java using packages, we can use:
• rJava
• Rserve

Use Cases where Integration plays a vital role:

Clustering, Classification or Regression analysis written in R script can originate from legacy implementation or conscious decisions to use R for certain use cases. Ideally, if you are developing a web application in Java, where you need some demand prediction or classification result and you want to show it in your User Interface (UI), then this is the integration you are looking for –

Integration of R and java using package ‘rJava’

rJava is a simple R-to-Java interface. It is comparable to the .C/.Call C interface. rJava provides a low-level bridge between R and Java (via JNI). It allows to create objects, call methods and access fields of Java objects from R.  JRI is a Java/R Interface providing a Java API to R functionality. A JavaDoc specification of this interface could be found at org.rosuda.JRI.Rengine.

 

Pre-requisites:

  1.  OS :Windows 8 64 bit.
  2. JDK: Version 1.8
  3. Eclipse: Luna or Mars
  4. R Workbench or RStudio:This is the GUI used to run R scripts. Here I am using R.3.2.2

 

Configuring R

Step 1:

Simply install the R workbench.  

Step 2:

Go to your R-Workbench or R-Studio and install rJava package using the command  install.packages(“rJava”)

Step 3:  

Now configure the PATH Variable for rJava

Step 4:  

Now adjust settings in Eclipse using the following steps-

  • Open eclipse LUNA.
  • Create a Java Project in eclipse named TestRJava
  • In the Package Explorersection right click on the project and select Build Path > Configure Build Path.

Step 5:

Select Add External JARs button on the right.

Browse to location <YOUR_R_HOME>\library\rJava\jri and select all 3 JAR files i.e. JRI.jar, JRIEngine.jar, REngine.jar and click Open > Ok.

Step 6:

Now under the src folder in Package create a package test

Step 7:

Inside test create a class by name Hello.java and execute the test code as shared below!

 

package test;

import java.io.IOException;

import org.rosuda.JRI.REXP;

import org.rosuda.JRI.Rengine;

public class Hello{

 

public static void main(String a[]) throws IOException {

// Start Rengine.

Rengine re= new Rengine(new String[] { “–no-save” }, false, null);

re.eval(String.format(“g <- ‘%s'”, “Hello R…”));

REXP result = engine.eval(“g”);

System.out.println(“Greeting R test …: “+result);

}

}

 

Got it done? You have just run a simple R and Java integrated code! Happy Coding!