Using R Script in Java

Go down

Using R Script in Java Empty Using R Script in Java

ตั้งหัวข้อ by Admin on Wed Jun 04, 2014 11:04 am

R is a language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment which was developed at Bell Laboratories (formerly AT&T, now Lucent Technologies) by John Chambers and colleagues. R can be considered as a different implementation of S. There are some important differences, but much code written for S runs unaltered under R.
R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, ...) and graphical techniques, and is highly extensible. The S language is often the vehicle of choice for research in statistical methodology, and R provides an Open Source route to participation in that activity.
One of R's strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formula where needed. Great care has been taken over the defaults for the minor design choices in graphics, but the user retains full control.
R is available as Free Software under the terms of the Free Software Foundation's GNU General Public License in source code form. It compiles and runs on a wide variety of UNIX platforms and similar systems (including FreeBSD and Linux), Windows and MacOS.
R script can be called from other programming tools, Java, VB, C#, etc. This article will provide technique how Java calling and using data from R.
1. Download and install R. (from
2. Set environment R_HOME
Using R Script in Java R_home
3. Download JRI library. (from
4. Open Java IDE. (I use Eclipse)
Using R Script in Java Eclipes
5. Create new Java project
6. Select JRE. (I use 64 bit)
Using R Script in Java Jre
7. Add JRI library to the Java project
Using R Script in Java Jri
8. Copy DLL to Java project folder (depend on JRE 32 or 64 bit)
This is source folder
Using R Script in Java Dll_src
This is Java project folder
Using R Script in Java Dll_dest
Note: Someone may be able to set the IDE to call the DLL from the source folder.
9. Now you are ready to use R script in Java.
package jri.demo;

import java.util.Enumeration;

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

* @author
public class TestJRI {

   public static void main(String[] args) {
       new TestJRI();

   private Rengine re;
   private REXP result;

   public TestJRI() {
       // Checking for the right version
       if (!Rengine.versionCheck()) {
          System.err.println("Error: Version mismatch, Java files don't match library version.");
       System.out.println("Creating Rengine");
       // Initial the Rengine
       // 1) Set the the arguments from the command line as null
       // 2) I don't use the main loop at first, so that I pass the second argument as "false"
       // 3) I don't require call back, but will process them in Java command.
       re = new Rengine(null, false, null);
       System.out.println("Rengine created");
       // Waiting until R start ready
       if (!re.waitForR()) {
           System.out.println("Cannot load R");
       System.out.println("R is ready for use");
       // You are ready to run R script now
       re.eval(String.format("greeting <- '%s'", "Hello R World"));
       result = re.eval("greeting");
       System.out.println("Greeting from R: "+result.asString());
       result = re.eval("x <- c(1,2,3,8,9)");
       System.out.println("Dataset = "+result);
       System.out.println("mean = "+result.asDouble());
       System.out.println("mean+1.5 = "+(result.asDouble()+1.5));
       System.out.println("Rengine closed");
10. Result from Java printout
Creating Rengine
Rengine created
R is ready for use
Greeting from R: Hello R World
Dataset = [REAL* (1.0, 2.0, 3.0, 8.0, 9.0)]
mean = 4.6
mean+1.5 = 6.1
Rengine closed
For RCaller need to download library from
Then add library to the project.
package jri.demo;

import rcaller.RCaller;    
import rcaller.RCode;

* @author
public class TestRCaller {

 public static void main(String[] args) {        
   new TestRCaller();    

 public TestRCaller() {        
   try {            
     RCaller caller = new RCaller();            
     caller.setRscriptExecutable("C:/Program Files/R/R-3.1.0/bin/x64/Rscript.exe");            

     RCode code = caller.getRCode();

     code.addRCode("x <- c(1,2,3,8,9)");
     code.addRCode("m <- mean(x)");

     double[] mean = caller.getParser().getAsDoubleArray("m");
     System.out.println("mean = "+mean[0]);        

   } catch (Exception e) {            

Some more RCaller code example


จำนวนข้อความ : 13
Join date : 29/01/2014

ขึ้นไปข้างบน Go down


Permissions in this forum: