zoukankan      html  css  js  c++  java
  • 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

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

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

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

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

  • 相关阅读:
    git 码云
    keras训练cnn模型时loss为nan
    将矩阵补齐0
    将dataframe分割为训练集和测试集两部分
    另存了一次网页之后其它word打开格式都变了
    python 判断字符串是否为(或包含)IP地址
    为多维数组添加一列以及reshape用法注意
    memory error python报错
    列表转换为三维矩阵
    LaTeX参考文献出现问号
  • 原文地址:https://www.cnblogs.com/emanlee/p/13946439.html
Copyright © 2011-2022 走看看