/ #R #JAVA 

Call R From Java Using JRI Library

R is a very powerful language designed for mathematics and statistics. It became one of the best data analysis tools due to its performance and its syntax simplicity. R can import datasets or generate a csv file with 1 line of code.

So, It will be very important to use R with one of the most known programming languages : JAVA. Using JRI library, i will show you how to assign variables, run R scripts…. in Java.

Initializing R engine

String[] arg = new String[]{"--vanilla"};
Rengine rengine= new Rengine(arg, false, null);

Double variable

eval(“command”) method will execute the command in R engine.

rengine.eval("x=13.5");
System.out.println(rengine.eval("x").asDouble());
// output
// 13.5

DataFrame

REXP is the default class for the result object. If you want to convert the result to Double, Int, Boolean.. you must use REXP methods like: .asDouble(),.asInt(),asBool() …

rengine.eval("myData=data.frame(x=c('v','d'),y=c('e','f'))");
REXP dataFrame=rengine.eval("myData");
System.out.println(dataFrame); //Vector containing 2 Factors
System.out.println(dataFrame.asVector().at(1));
System.out.println(dataFrame.asVector().at(1).asFactor().at(1));

// output
// [VECTOR ([FACTOR {levels=("d","v"),ids=(1,0)}], [FACTOR {levels=("e","f"),ids=(0,1)}])]
// [FACTOR {levels=("e","f"),ids=(0,1)}]
// f

RBool

RBool bool=rengine.eval("1==0").asBool();
System.out.println(bool);

// output
// FALSE

Running R script

rengine.eval("source(\"path/to/your/script\")");