Home:ALL Converter>Run R script using JAVA program

Run R script using JAVA program

Ask Time:2015-09-03T18:55:15         Author:Tiya

Json Formatter

I am new to R programming. I have created a simple R script and trying to run it using JAVA class, but I am unable to do it.

I have tried by using Rserve as well as rJava. Using Rserve, code execution stopped after creating instance of "RConnection" whereas using rJava giving exception "java.lang.UnsatisfiedLinkError: jri.dll: Can't find dependent libraries".

The JAVA class code is as below:

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 my question is not clear to you or if you want to know anything else. Thanks in advance.

Author:Tiya,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32373680/run-r-script-using-java-program
jfcorugedo :

There are two different approach to connect Java and R.\n\nIf you want to use JRI, you have to start your java program using the JVM parameter -Djava.library.path pointing at the folder that contains JRI library.\n\nFor instance:\n\n$JAVA_HOME/bin/java -Djava.library.path=/app/vendor/R/lib/R/library/rJava/jri/ -jar target/myapp.jar\n\n\nIf you have trouble finding JRI installation directory, try to look for the JRI SO library:\n\nfind / -name \"libjri.*\"\n\n\nIn addition, make sure you have created R_HOME and LD_LIBRARY_PATH in your environment:\n\n\nR_HOME: Pointing to your local R installation (Ej: /Library/Frameworks/R.framework/Resources)\nLD_LIBRARY_PATH: Pointing to R lib directory as well as JRI directory (EJ: $LD_LIBRARY_PATH:/app/vendor/R/lib/R/lib:/app/vendor/R/lib/R/bin)\n\n\nOn the other hand, if you want to use Rserve, you need to start Rserve in a separate process, and then create a RConnection from your java process.\n\nFor example:\n\n if(LOGGER.isInfoEnabled()) {\n LOGGER.info(\"Starting RServe process...\");\n }\n 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));\n builder.inheritIO();\n Process rProcess = builder.start();\n\n if(LOGGER.isInfoEnabled()) {\n LOGGER.info(\"Waiting for Rserve to start...\");\n }\n int execCodeResult = rProcess.waitFor();\n\n if(execCodeResult != SUCCESS_CODE) {\n LOGGER.error(String.format(\"Unexpected error code starting RServe: %d\", execCodeResult));\n } else {\n LOGGER.error(\"RServe started successfully\");\n }\n\n if(LOGGER.isInfoEnabled()) {\n LOGGER.info(\"Opening connection to RServe daemon....\");\n }\n REngine engine = new RConnection();\n if(LOGGER.isInfoEnabled()) {\n LOGGER.info(String.format(\"Obtaining R server version: %d\", ((RConnection)engine).getServerVersion()));\n }\n //Perform some engine.parseAndEval(\"....\");\n\n\nrserveConf is the path to Rserv conf file and rexe is the full path to R executable.\n\nFor instance, in my MacOS computer I can start Rserve executing this line:\n\n/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\"\n\n\nThis command outputs something like this:\n\nStarting Rserve:\n /Library/Frameworks/R.framework/Resources/bin/R CMD /Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rserve/libs//Rserve --slave \n\nRserv started in daemon mode.\n\n\nMake sure to specify \"--slave\" parameter when you start Rserve.\n\nIf you want to see more examples, I have a demo project that use both approaches, JRI and RServe, in my github:\n\nhttps://github.com/jfcorugedo/RJavaServer",
2015-09-23T09:56:24
Malaka Gunawardhana :

You just want to call an external application: wouldn't the following work?\n\nRuntime.getRuntime().exec(\"Rscript myScript.R\"); \n\n\nCredit goes to stackoverflow itself",
2015-09-03T11:04:22
yy