Wednesday, August 26, 2015

R and JRI Integration with Java for Analytics

Different ways(rJava with JRI, RServ, RCaller, Renjin) are there to integrate R with Java to perform some analytic work inside the Java (Servlet / controller). In this post I would like to show, how you can integrate rJava and JRI with java to run R code inside Java.

JRI Integration with Java: JRI stands for Java-R Interface and It is a part of rJava. Using rJava we can manipulate Java objects inside R using JNI (Java Native Interface) which is the natural way of calling compiled binary code in Java but JRI is reverse of it. In JRI we can call R inside Java. This library has some merit among others, in means of full interoperability between R and Java using callbacks. It is robust for large scale projects, however, it is painless at start-up process for relatively small projects.

To Install R on debian based systems run the following command
sudo apt-get install r-base-dev

Now to install rJava package in R run the following command
sudo apt-get install r-cran-rjava

Other wise you can run following command in R to install rJava
install.packages("rJava")

Once you install the R and rJava, to find the location of JRI inside rJava run the following command in R.
system.file("jri",package="rJava")







Now export R_HOME=/usr/lib/R, LD_LIBRARY_PATH=/usr/lib/R/site-library/rJava/jri into environment and add both of them in the Java library path also.

Now open your eclipse create a simple Java project "JRITest" with the below mentioned code. Before running the code in eclipse set R_HOME and LD_LIBRARY_PATH environment variables in eclipse. For setting up environment variables in eclipse

Right Click on Project -> Run As -> Run Configurations -> Environments then add environment variables with their values as shown below.





Sample Code To Test JRI In Java:
Sample Input Output:


No comments:

Post a Comment