Thursday, February 15, 2018

How to Call R from Java using Rserve, R Programming through Java

We will use Rserve software to connect our Java code to R environment. Rserve is a TCP/IP server which allows other programs to use facilities of R  from various languages without the need to initialize R or link against R library. Every connection has a separate workspace and working directory. For more details please refer this page

  1. Install R software in your computer somewhere. (My case it is D:\InstalledSoftware\R-3.4.3)
  2. Start your R environment
  3. Install Rserve package (use command install.packages("Rserve"))
  4. Start Rserve in your R environment (use command library(Rserve))
  5. That’s it from R side.
  6. Now setup your Java project.
  7. Open any Eclipse, create java project say "RJavaConnect"
  8. Add two Rserve related jar file in your RJavaConnect project (REngine.jar and Rserve.jar)
  9. You can get both jar from D:\InstalledSoftware\R-3.4.3\library\Rserve\java (In your case check your R installation path). Else you can add these two dependency in your pom.xml

                         Finally, create one Java Class say and execute below code. That’s it :)

                      package pkg;

                      import org.rosuda.REngine.REXP;
                      import org.rosuda.REngine.REXPMismatchException;
                      import org.rosuda.REngine.REngineException;
                      import org.rosuda.REngine.Rserve.RConnection;
                      import org.rosuda.REngine.Rserve.RserveException;

                      public class Demo {

                      RConnection connection = null;

                      public static void main(String[] args) {
                      Demo demo = new Demo();

                      public void connectToR(){

                      connection = new RConnection();
                      String vector = "c(1,2,3,4)";
                                  connection.eval("meanVal=mean(" + vector + ")");
                                  double mean = connection.eval("meanVal").asDouble();
                                  System.out.println("The mean of given vector is=" + mean);
                                   String setwd = "setwd(\"d:/InstalledSoftware/RStudio\")";
                                   vector = "binod<-c p="">
                                  int[] input = {10,20,30,40,50};
                                  connection.assign("input", input);
                                  REXP output = connection.eval("mean(input)");
                                  System.out.println("Mean of out input data :"+output);
                                  System.out.println("Mean of out input data :"+output.asDouble());
                                  }catch (RserveException e) {
                                 }catch (REXPMismatchException e) {
                                } catch (REngineException e) {
                                       if(connection != null){


                      No comments:

                      Post a Comment

                      You can put your comments here (Either feedback or your Question related to blog)