簡體   English   中英

Java和R橋

[英]Java and R bridge

我想從Java運行R腳本。 我有以下代碼,但給出空值:

try {
    RCaller caller = new RCaller();
    caller.setRscriptExecutable("C:/Program Files/R/R-3.0.1/bin/x64/Rscript.exe");
    caller.cleanRCode();              
    caller.addRCode("k<-1");    //Initializing k to 1
    caller.addRCode("b<-print(k)"); 
    caller.runAndReturnResult("b"); //This should output the value of b      
} catch(Exception e) {
    e.printStackTrace();
}

我不知道我在做什么錯。 請幫忙。

從“程序文件”路徑,我將使您在Windows上工作。 如果是這樣,則您的問題很可能是路徑上的斜線:

caller.setRscriptExecutable("C:/Program Files/R/R-3.0.1/bin/x64/Rscript.exe");

嘗試以下方法:

caller.setRscriptExecutable("C:\\Program Files\\R\\R-3.0.1\\bin\\x64\\Rscript.exe");

我建議您下載最新版本2.1.1。 以下代碼在2.1.1版中按預期工作(打印1 )。

import rcaller.RCaller;
import rcaller.RCode;

public class RCallerDemo {
    public static void main(String[] args) {
        try {
            RCaller caller = new RCaller();
            caller.setRscriptExecutable("/usr/bin/Rscript");
            caller.cleanRCode();
            RCode code = new RCode();
            final String st1 = "k<-1";
            final String st2 = "b<-print(k)";
            code.addRCode(st1);
            code.addRCode(st2);
            caller.setRCode(code);    //Initializing k to 1
            caller.runAndReturnResult("b"); //This should output the value of b
            int b = caller.getParser().getAsIntArray("b")[0];
            System.out.println(b);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

該示例基於原始RCaller示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM