24

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 4

24

While I'm unaware of a 'converter', there is an interface called rJava which will allow you to run R code directly from Java.

http://www.rforge.net/rJava/

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.

Sign up to request clarification or add additional context in comments.

Comments

11

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

2

As @Seidr said, using rJava we can run R code directly from Java methods.

  1. Open R console and install rJava package

    install.packages("rJava")
    

    Now you will find rJava in "R home\library"

  2. Now in eclipse, add JRI.jar, JRIEngine.jar, REngine.jar to project build path. These jars are available in "R home\library\rJava\jri"

  3. Create Rengine object.
  4. 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

Welcome to SO. Please be advised that repeating answers given earlier and "link only" answers are generally frowned upon. Instead of just giving the link, summarize the steps in your answer and refer with the link to the original source. That way the answer doesn't become invalid when the link disappears. Also: that tutorial is incomplete (as explained further in the comments on that blog).
1

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.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.