[英]Java and R bridge
I want to run r-script from java. 我想从Java运行R脚本。 I have the following code, but giving null: 我有以下代码,但给出空值:
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();
}
I don't know what I'm doing wrong. 我不知道我在做什么错。 Please help out. 请帮忙。
From the "Program Files" path, I'll get you're working on Windows. 从“程序文件”路径,我将使您在Windows上工作。 If so, there is a chance that your problem are the slashes on the path: 如果是这样,则您的问题很可能是路径上的斜线:
caller.setRscriptExecutable("C:/Program Files/R/R-3.0.1/bin/x64/Rscript.exe");
Try this instead: 尝试以下方法:
caller.setRscriptExecutable("C:\\Program Files\\R\\R-3.0.1\\bin\\x64\\Rscript.exe");
I suggest you download the latest version, 2.1.1. 我建议您下载最新版本2.1.1。 The code below works as expected (prints 1
) with version 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();
}
}
}
The example is based on the original RCaller examples . 该示例基于原始RCaller示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.