using R programming in java

后端 未结 4 1697
一向
一向 2020-12-25 13:11

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 Ja

4条回答
  •  抹茶落季
    2020-12-25 13:35

    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/

提交回复
热议问题