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

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

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

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

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

  • 相关阅读:
    初入博客
    winsocket入门学习
    理解音视频 PTS 和 DTS
    理解音视频 PTS 和 DTS
    FFMPEG学习----使用SDL构建音频播放器
    FFmpeg被声明为已否决的解决方案
    FFMPEG结构体分析:AVCodecParameters
    FFMPEG学习----使用SDL播放PCM数据
    如何提取CSDN博客正文内容
    遍历CSDN博客
  • 原文地址:https://www.cnblogs.com/emanlee/p/13946439.html
Copyright © 2011-2022 走看看