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

ε祈祈猫儿з 提交于 2020-01-05 06:39:25

问题


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.

call code in Java:

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

last line from R code:

eq_all[length(eq_all)-1]

--

output in Java console:

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

"3.050462038715372" is the right value but how can I access it in Java?

best regards, Immanuel

PS. related question without answer: Converting REXP object to a double array (Java/R)


回答1:


I believe asDouble() and asDoubleArray() are what you need.

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. The referred question actually had the answer you needed—the problem there is with implementation of toString() in Java arrays.




回答2:


elementAt() is not working, your could use at().

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


来源:https://stackoverflow.com/questions/8029601/call-r-from-java-using-jri-how-to-cast-return-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!