R Tutorial: How to Integrate R With Java Using rJava

JAVA

Why use R with Java?

Modern enterprise applications are increasingly using latest research techniques to obtain maximum return on investment. Analytics plays an important role in the success of any organization in the market. Analytics could help improve business in various domains like: improving profits, increasing market value, increasing customer satisfaction etc. Using analytics in any enterprise application boils down to using statistical and mathematical techniques to quantify various business domains.

Modern programming languages that are mainly used to develop enterprise software systems include: Java, C/C++, Ruby etc. These platforms have rich functionality to write business logic, however they are not much efficient when it comes to statistical or mathematical modelling. In the field of modelling, the major contributors are: R, Weka, Octave etc. Out of these most work as simulation environments, however R could be used both, for simulation as well as for production level systems.

From the above discussion it is clear that an Intelligence based software could not be developed just by using a single technology. To overcome this obstacle a combination of technologies should be used. The figure below shows a high level view of such an intelligent software system and where each technology fits.

integration of R with JavaFig. 1

From above figure Fig. 1 it is clear that a hybrid system has to be created. In the current scenario the hybrid system consists of JAVA for business logic programming and R for statistical programming (to get a basic understanding about R you could refer R(wikipedia), CRAN). This shows that we have a need to integrate R with Java, which is the main theme of this post. In the following text we will be showing how to integrate R with Java using rJava library.

How to Integrate R with Java using rJava package?

Integration of R with Java could be achieved in many ways. Main R packages are:

  1. rJava
  2. Rserve (integration with rJava is discussed in R Tutorial: How to integrate R with Java using Rserve tutorial)

Both packages have their own properties, however the main use-case specific properties are:

  1. If you need to embed basic R code snippets in Java code then use rJava.
  2. If you need to create an R server which accepts request from Java code and return response back to Java, then use Rserve.

In this tutorial we will be discussing the usage of rJava package.

Integration of R with Java using package rJava

Pre-requisites

For the purpose of this tutorial we are using following components:

  1. Operating System: Windows 7, 32 bit.
  2. JDK: Version 1.7 or above.
  3. Eclipse: Luna.
  4. R Workbench: This is the GUI used to run R scripts. We are using R 3.1.3 you could download the latest version from the same link.

 Configuring R

Simply install the R workbench downloaded above. Try installing the workbench at a location other than C:\ drive as it has some permission issues. For the purpose of this tutorial R workbench is installed in D:\ProgramFiles\R directory.

Integration Steps

Step-1 (Installing rJava package)

Open you R workbench and type following command in R console

install.packages('rJava')

The console will display a small window with CRAN mirror header. Choose a mirror from which rJava package will be installed (for the purpose of this tutorial we have used mirror USA (KS)). After the package is successfully installed you will see a screen something similar to below figure.

installing rJava

Fig. 2

Step-2 (Configuring PATH variable for rJava)

In order to use rJava package inside Java code we need to add few location to the PATH environment variable. Follow following steps to configure PATH:

  1. Right click on My Computer and select Properties.
  2. In the System window that appears, select Advanced system settings.
  3. System Properties dialog will appear, on this dialog select Environment Variables.
  4. Under System variables section search for variable PATH. Select PATH variable and click on Edit button.
  5. In the  Variable value section append ;D:\ProgramFiles\R\R-3.1.3\bin\i386;D:\ProgramFiles\R\R-3.1.3\library\rJava\jri; (see the ; at the start is necessary to separate current paths from existing paths). In this step we have added two paths, one corresponding to R dlls and other corresponding to rJava dlls. Note: As I have installed R in D:\ProgramFiles\R all my paths contain this location for your location the generic path structure should be like ;<YOUR_R_HOME>\bin\i386;<YOUR_R_HOME>\library\rJava\jri\i386;
  6. Click Ok, Ok, Ok.

That is all needed to configure the path.

Step-3 (Creating JAVA Program)

Now that we have configured R, we need to proceed with creating Java code that will be using R functionality.

  1. Open eclipse LUNA.
  2. Create a Java Project in eclipse named RwithJAVA.
  3. In the Package Explorer section right click on the project and select Build Path > Configure Build Path.5Fig. 3
  4. In window titled Properties for RwithJAVA select Libraries tab6Fig. 4
  5. Select Add External JARs button on the right. Browse to location D:\ProgramFiles\R\R-3.1.3\library\rJava\jri or in your case <YOUR_R_HOME>\library\rJava\jri and select all 3 JAR files i.e. JRI.jar, JRIEngine.jar, REngine.jar and click Open > Ok.
  6. Now the structure of your project in eclipse should be like below:rJava jar files                                                        Fig. 5
  7. Now under the src folder in Package create a package pkg and inside pkg create a class Temp.java

That is all we need to configure a Java project. Next we need to write the actual code.

Step-4 (JAVA code)

We will be using a scenario where we have a R vector c(1,2,3,4,5) declared as Java string, and we need to calculate the average of this vector using mean() function of R. Following is the code snippet to show the functionality.

package pkg;

import org.rosuda.JRI.Rengine;

public class Temp {

    public static void main(String a[]) {

        // Create an R vector in the form of a string.
        String javaVector = "c(1,2,3,4,5)";

        // Start Rengine.
        Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);

        // The vector that was created in JAVA context is stored in 'rVector' which is a variable in R context.
        engine.eval("rVector=" + javaVector);
        
        //Calculate MEAN of vector using R syntax.
        engine.eval("meanVal=mean(rVector)");
        
        //Retrieve MEAN value
        double mean = engine.eval("meanVal").asDouble();
        
        //Print output values
        System.out.println("Mean of given vector is=" + mean);

    }
}

Step-5 (Final output)

As the contents of the vector are 1,2,3,4,5 so the average should be (1+2+3+4+5)/5=3.0, the final output should be Mean of given vector is=3.0