We are working on a complex statistical project on Java. We did the original code in the R programming language. Is there a way to convert this code to Java code (converter) or otherwise how can we use R in a Java project?
4 Answers
While I'm unaware of a 'converter', there is an interface called rJava which will allow you to run R code directly from Java.
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.
In a sense the inverse of rJava is JRI (Java/R Interface) which provides the opposite direction - calling R from Java. JRI is now shipped as a part of the rJava package, although it still can be used as a separate entity (especially for development). Currently rJava is used as a part of JGR, iPlots and JavaGD software/packages.
Comments
You can try Renjin (http://www.renjin.org):
"Renjin is a JVM-based interpreter for the R language for statistical computing. This project is an initiative of BeDataDriven, a company providing consulting in analytics and decision support systems."
Comments
As @Seidr said, using rJava we can run R code directly from Java methods.
Open R console and install rJava package
install.packages("rJava")Now you will find rJava in "R home\library"
Now in eclipse, add
JRI.jar,JRIEngine.jar,REngine.jarto project build path. These jars are available in "R home\library\rJava\jri"- Create Rengine object.
Now create two vectors, add them and store in another variable. Now print that result variable in console.
Rengine engine = new Rengine(new String[]{"--no-save"},false,null); String aVector = "c(1,2,3)"; String bVector = "c(4,5,6)"; engine.eval("a<-"+aVector); engine.eval("b<-"+bVector); engine.eval("c<-a+b"); System.out.println("Sum of two vectors : c = "+engine.eval("c"));
Hope below link helps (step-by-step procedure to integrate R in Java).
http://www.codophile.com/how-to-integrate-r-with-java-using-rjava/
1 Comment
The another option to run R on JVM is FastR. According FastR's documentation:
FastR is an implementation of the R Language in Java atop Truffle, a framework for building self-optimizing AST interpreters.
FastR is currently available in two forms:
- As a pre-built binary with the Truffle implementations of Ruby and JavaScript for Linux and Mac OS X. Unfortunately, there is no Windows version available yet.
- As a source release on GitHub. This option does not come with Ruby or JavaScript.