Java调用R语言脚本

 

Check out the JRI/rJava project which provides a Java/R interface. After you download the package, see the examples directory.

 

Another option is to use the RCaller library (though I've never tried it myself...this question suggests potential performance issues).

 

REF

https://stackoverflow.com/questions/8509120/how-can-i-use-java-to-output-an-r-graph

 

===============

String rScriptFileName = rPath+"My_R_Script.R";

Runtime.getRuntime().exec("mypathto\\R\\bin\\Rscript "+rScriptFileName);

===============

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

===============

 

Process child = Runtime.getRuntime().exec(command, environments, dataDir);

int code = child.waitFor();

switch (code) {
    case 0:
        //normal termination, everything is fine
        break;
    case 1:
        //Read the error stream then
        String message = IOUtils.toString(child.getErrorStream());
        throw new RExecutionException(message);
}

===============

 BufferedReader reader = null;

Process shell = null;

try {

shell = Runtime.getRuntime().exec(new String[]

{ "/usr/bin/Rscript", "/media/subin/works/subzworks/RLanguage/config/predict.R" });

reader = new BufferedReader(new InputStreamReader(shell.getInputStream()));

String line;

while ((line = reader.readLine()) != null)

{ System.out.println(line); } }

catch (IOException e)

{ e.printStackTrace(); }

===============

 You might want to take a look at these three projects.

===============

 

You can also use FastR, which is R engine implemented on top of JVM. Using it from Java is as simple as:

Context context = Context.newBuilder("R").allowAllAccess(true).build();
int result = context.eval("R", "sum").execute(new int[] {3,4,5}).asInt();
context.eval("R", "print('you can eval any R code here');");

===============

 What worked for me was to use the Renjin interpreter Download Renjin

 ===============

 

 ===============

 

 ===============

 

 

 ===============

posted @ 2020-11-08 23:22  emanlee  阅读(324)  评论(0编辑  收藏  举报