Run R script using JAVA program

I am new to R programming. I created a simple R script and try to run it with a JAVA class, but I cannot do this.

I tried using rserve as well as rJava. Using Rserve, code execution stopped after creating an instance of "RConnection", while using the rJava exception "java.lang.UnsatisfiedLinkError: jri.dll: cannot find dependent libraries".

The JAVA class code is as follows:

For rJava:

    import org.rosuda.JRI.Rengine;
public class Temp {

public static void main(String a[]) {
    // Create an R vector in the form of a string.
    String javaVector = "c(1,2,3,4,5)";

    // Start Rengine.
    Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);

    // The vector that was created in JAVA context is stored in 'rVector' which is a variable in R context.
    engine.eval("rVector=" + javaVector);

    //Calculate MEAN of vector using R syntax.
    engine.eval("meanVal=mean(rVector)");

    //Retrieve MEAN value
    double mean = engine.eval("meanVal").asDouble();

    //Print output values
    System.out.println("Mean of given vector is=" + mean);

}
}

For Rserve:

import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
public class Temp {

public static void main(String a[]) {
    RConnection connection = null;
    System.out.println("line 10");
    try {
        // Create a connection to Rserve instance running on default port 6311

        System.out.println("line 15");
        connection = new RConnection();
        System.out.println("line 17");
        //Note four slashes (\\\\) in the path
        connection.eval("source('D:\\\\RExamples\\\\helloworld.R')");
        System.out.println("line 19");
        int num1=10;
        int num2=20;
        int sum=connection.eval("myAdd("+num1+","+num2+")").asInteger();
        System.out.println("The sum is=" + sum);
    } catch (RserveException e) {
        e.printStackTrace();
    } catch (REXPMismatchException e) {
        e.printStackTrace();
    }
}
}

Please let me know if you do not understand my question, or if you want to know something else. Thanks in advance.

+4
source share
3 answers

: ?

Runtime.getRuntime().exec("Rscript myScript.R"); 

0

Java R.

JRI, java-, JVM -Djava.library.path, , JRI.

:

$JAVA_HOME/bin/java  -Djava.library.path=/app/vendor/R/lib/R/library/rJava/jri/ -jar target/myapp.jar

JRI, JRI SO:

find / -name "libjri.*"

, , R_HOME LD_LIBRARY_PATH :

  • R_HOME: R (Ej:/Library/Frameworks/R.framework/Resources)
  • LD_LIBRARY_PATH: R lib, JRI (EJ: $LD_LIBRARY_PATH:/app/vendor/R/lib/R/lib:/app/vendor/R/lib/R/BIN)

, Rserve, Rserve , RConnection java-.

:

        if(LOGGER.isInfoEnabled()) {
            LOGGER.info("Starting RServe process...");
        }
        ProcessBuilder builder = new ProcessBuilder("/bin/sh", "-c", String.format("echo 'library(Rserve);Rserve(FALSE,args=\"--no-save --slave --RS-conf %s\")'|%s --no-save --slave", rserveConf, rexe));
        builder.inheritIO();
        Process rProcess = builder.start();

        if(LOGGER.isInfoEnabled()) {
            LOGGER.info("Waiting for Rserve to start...");
        }
        int execCodeResult = rProcess.waitFor();

        if(execCodeResult != SUCCESS_CODE) {
            LOGGER.error(String.format("Unexpected error code starting RServe: %d", execCodeResult));
        } else {
            LOGGER.error("RServe started successfully");
        }

        if(LOGGER.isInfoEnabled()) {
            LOGGER.info("Opening connection to RServe daemon....");
        }
        REngine engine = new RConnection();
        if(LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("Obtaining R server version: %d", ((RConnection)engine).getServerVersion()));
        }
        //Perform some engine.parseAndEval("....");

rserveConf - conf Rserv rexe - R.

, MacOS Rserve, :

/bin/sh -c "echo 'library(Rserve);Rserve(FALSE,args=\"--slave --RS-conf /Users/me/Documents/test/rserve.conf\")'|/Library/Frameworks/R.framework/Versions/3.2/Resources/bin/exec/R --no-save --slave"

- :

Starting Rserve:
  /Library/Frameworks/R.framework/Resources/bin/R CMD /Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rserve/libs//Rserve --slave 

Rserv started in daemon mode.

"-slave" Rserve.

, , , JRI RServe, github:

https://github.com/jfcorugedo/RJavaServer

+2

An alternative is to use the r OpenCpu package. This is a very simple server (unfortunately, a single thread) that receives r-function calls via http and returns the result as an answer.

I used it to interact with java.

0
source

Source: https://habr.com/ru/post/1605714/


All Articles