[英]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.