[英]Issue in R arules package using java
For a university project I have to implement arules(package of R) in java. 对于一个大学项目,我必须在Java中实现arules(R包)。 I have successfully integrated R and java using JRI.
我已经使用JRI成功集成了R和Java。 I did not understand how to get output of "
inspect(Groceries[1:1])
". 我不明白如何获取“
inspect(Groceries[1:1])
”的输出。 I have tried with asString(),asString[]()
but this gives me following error: 我已经尝试过
asString(),asString[]()
但这给了我以下错误:
Exception in thread "main" java.lang.NullPointerException
at TestR.main(TestR.java:11)
Also, how can implement summary(Groceries)
in java? 另外,如何在Java中实现
summary(Groceries)
? How to get output of summary in String array or string? 如何获取字符串数组或字符串中的摘要输出?
R code: R代码:
>data(Groceries)
>inspect(Groceries[1:1])
>summary(Groceries)
Java code: Java代码:
import org.rosuda.JRI.Rengine;
import org.rosuda.JRI.REXP;
public class TestR {
public static void main(String[] args){
Rengine re = new Rengine(new String[]{"--no-save"}, false, null);
re.eval("library(arules)");
re.eval("data(Groceries)");
REXP result = re.eval("inspect(Groceries[1:1])");
System.out.println(result.asString());
}
}
Appears that the inspect
function in pkg:arules returns NULL. 似乎pkg:arules中的
inspect
函数返回NULL。 The output you see is a "side-effect". 您看到的输出是“副作用”。 You can attempt to "capture output" but this is untested since I don't have experience with this integration across languages.
您可以尝试“捕获输出”,但这未经测试,因为我没有跨语言集成的经验。 Try instead.:
请尝试。
REXP result = re.eval("capture.output( inspect(Groceries[1:1]) )");
In an R console session you will get: 在R控制台会话中,您将获得:
library(arules)
data("Adult")
rules <- apriori(Adult)
val <- inspect(rules[1000])
> str(val)
NULL
> val.co <- capture.output(inspect(rules[1000]))
> val.co
[1] " lhs rhs support confidence lift"
[2] "1 {education=Some-college, "
[3] " sex=Male, "
[4] " capital-loss=None} => {native-country=United-States} 0.1208181 0.9256471 1.031449"
But I haven't tested this in a non-interactive session. 但是我还没有在非交互会话中对此进行测试。 May need to muck with the
file
argument to capture.output
, ... or it may not work at all. 可能需要将
file
参数设置为capture.output
,...,否则可能根本无法工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.