我正在使用 RCaller 从 Java 调用 R。请参阅以下 Java 代码:
RCaller caller = new RCaller();
RCode code = new RCode();
caller.setRscriptExecutable("/usr/bin/Rscript");
caller.cleanRCode();
String x = "is.installed <- function(mypkg){ \n" +
"is.element(mypkg, installed.packages()[,1])\n"+
"}\n" +
"is.installed(\"bbmle\")";
StringBuffer s = new StringBuffer(x);
code.setCode(s);
System.out.println(x);
caller.setRCode(code);
caller.redirectROutputToConsole();
caller.runOnly();
这是 Java 输出。请注意 Rcaller 如何决定在 R 代码末尾添加一个 q
(请参阅下面输出中的倒数第二行)。这是怎么回事?请注意,当我打印输出时,那里没有 q
。
is.installed <- function(mypkg){
is.element(mypkg, installed.packages()[,1])
}
is.installed("bbmle")
Error:Error: unexpected symbol in "is.installed("bbmle")q"
Error:Execution halted
最佳答案
RCaller 发送
q("yes")
命令R(在RunOnly()方法中),但是当你忘记在代码末尾放回车符时,并不是所有的R代码都可以发送到R(只有q部分被发送)发送)。使用
is.installed(\"bbmle\")\n
相反。
关于java - Rcaller 将 q 字符添加到 StringBuffer 的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21515155/