本文介绍了使用RServe奇怪错误从Java调用R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

import org.rosuda.REngine.Rserve.RConnection;


public class TestProgram {

    public static void main(String[] args) {

           try {

               RConnection rConnection = new RConnection(); 
               // make a new local connection on default port (6311)
               rConnection.eval("for(i in 1:.Machine$integer.max){}");
               System.out.println("Done!");

           }
           catch(Exception e) {
               System.out.println(e.toString());
           }

    }


}

我收到此异常:

org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127

如果我更改:

它确实有效:-)

有人知道发生了什么吗?

Does anyone know what's going on ?

P.S我使用以下命令从R(同一台机器)启动Rserve:

P.S I started Rserve from R ( same machine ) using :

>library(Rserve)
>Rserve()
> sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] Rserve_1.7-3

loaded via a namespace (and not attached):
[1] tools_3.0.1

OS是Windows8.我没有在Linux上尝试过.

OS is Windows 8. I did not try this on Linux.

推荐答案

您应该检查eval函数的返回值,看看它是否扩展了try-error.如果确实如此,则将其打印到调试字符串以获取错误消息.以下部分摘自Rserve文档.这将给您导致127的错误消息.您还应该使用parseAndEval而不是eval.

You should check the return from the eval function to see if it extends try-error. If it does then print it to debug string to get the error message. The section below was taken from the Rserve documentation. This will give you the error message that caused the 127. You should also probably use parseAndEval rather than just eval.

http://www.rforge.net/Rserve/faq.html

c.assign(".tmp.", myCode);
REXP r = c.parseAndEval("try(eval(parse(text=.tmp.)),silent=TRUE)");
if (r.inherits("try-error")) System.err.println("Error: "+r.toString())
else { // success .. }

如果这是您的R环境的限制,您可能还需要检查此链接.

You might also want to check this link in case it is a restriction of your R environment.

R-大数据-矢量超出矢量长度限制

修正克里斯·欣肖的答案

Fixing Chris Hinshaw's answer

c.assign(".tmp.", myCode);
REXP r = c.parseAndEval("try(eval(parse(text=.tmp.)),silent=TRUE)");
if (r.inherits("try-error")) System.err.println("Error: " + r.asString())
else { // success .. }

请注意,println应该使用asString(),而不是toString()

Note that the println should be using asString(), not toString()

这篇关于使用RServe奇怪错误从Java调用R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 20:19