简体   繁体   English

使用JRI从Java调用R,如何转换返回值

[英]Call R from Java using JRI, how to cast return value

I'm executing some R commands from Java using JRI.I want to use the results from R in my Java for further calculations but I have no idea how cast the returned object. 我正在使用JRI从Java执行一些R命令。我想将Java中R的结果用于进一步的计算,但是我不知道如何转换返回的对象。

call code in Java: Java中的调用代码:

REXP x;
System.out.println(x = rengine.eval("source(\"/..../TS.R\")"));
System.out.println( x.asVector().elementAt(0));

last line from R code: R代码的最后一行:

eq_all[length(eq_all)-1]

-- -

output in Java console: Java控制台中的输出:

[VECTOR ([REAL* (3.050462038715372)], [BOOLi* ])]
[REAL* (3.050462038715372)]

"3.050462038715372" is the right value but how can I access it in Java? “ 3.050462038715372”是正确的值,但是如何在Java中访问它?

best regards, Immanuel 最好的问候,伊曼纽尔

PS. PS。 related question without answer: Converting REXP object to a double array (Java/R) 没有答案的相关问题: 将REXP对象转换为双精度数组(Java / R)

elementAt() is not working, your could use at(). elementAt()无法正常工作,您可以使用at()。

REXP x;
System.out.println(x = rengine.eval("source(\"/..../TS.R\")"));
System.out.println(x.asVector().at(0).asDouble());

I believe asDouble() and asDoubleArray() are what you need. 我相信asDouble()asDoubleArray()是您所需要的。

Update: So on your code example, it should be: 更新:因此在您的代码示例中,它应该是:

REXP x;
System.out.println(x = rengine.eval("source(\"/..../TS.R\")"));
System.out.println(x.asVector().elementAt(0).asDouble());

PS. PS。 The referred question actually had the answer you needed—the problem there is with implementation of toString() in Java arrays. 所提到的问题实际上提供了您需要的答案-Java数组中toString()实现存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM