尝试以int形式访问org.rosuda.REngine.REXPGenericVector 我正在做一个项目,使用Java调用基于Rserve的R。当我在Java中执行R代码时,我得到一个错误。

尝试以int形式访问org.rosuda.REngine.REXPGenericVector 我正在做一个项目,使用Java调用基于Rserve的R。当我在Java中执行R代码时,我得到一个错误。,java,r,rserve,Java,R,Rserve,这是我的java代码: xp=c.eval("eperson(economic)"); System.out.println("----eperson(economic).asList()-----"); //System.out.println(xp.length()); for(int i=0;i<xp.asInteger();i++){ System.out.println(xp.asStrings

这是我的java代码:

    xp=c.eval("eperson(economic)");
    System.out.println("----eperson(economic).asList()-----");
    //System.out.println(xp.length());
    for(int i=0;i<xp.asInteger();i++){
       System.out.println(xp.asStrings()[i]);
 }
org.rosuda.REngine.REXPMismatchException: attempt to access org.rosuda.REngine.REXPGenericVector as int
> eperson(economic)
[[1]]
[1] "主成份分析结果为"

[[2]]
Importance of components:
                          Comp.1    Comp.2    Comp.3     Comp.4    Comp.5      Comp.6 Comp.7
Standard deviation     2.1124391 1.1491308 0.9331551 0.50448791 0.2699638 0.137595030      0
Proportion of Variance 0.6374856 0.1886431 0.1243969 0.03635829 0.0104115 0.002704627      0
Cumulative Proportion  0.6374856 0.8261287 0.9505256 0.98688388 0.9972954 1.000000000      1

Loadings:
   Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7
x1 -0.446  0.127  0.138  0.431  0.599  0.241 -0.404
x2 -0.467                0.268                0.837
x3 -0.445  0.146 -0.243        -0.671  0.451 -0.260
x4 -0.377 -0.377  0.168 -0.763  0.236  0.223       
x5 -0.257 -0.297 -0.819         0.162 -0.382       
x6 -0.418  0.280  0.337 -0.137 -0.196 -0.730 -0.208
x7         0.808 -0.319 -0.375  0.265         0.150

[[3]]
[1] "综合得分和经济人口承载力为"

[[4]]
  年份      指数  承载人口
1 2006 2.2589596 178.29831
2 2007 1.9662692 155.19644
3 2008 1.6925468 133.59169
4 2009 0.2778931  21.93393
5 2010 0.8682956  68.53405
6 2011 1.8017713 142.21272
7 2012 2.9307567 231.32285
我在RStudio中运行了代码,结果如下:

    xp=c.eval("eperson(economic)");
    System.out.println("----eperson(economic).asList()-----");
    //System.out.println(xp.length());
    for(int i=0;i<xp.asInteger();i++){
       System.out.println(xp.asStrings()[i]);
 }
org.rosuda.REngine.REXPMismatchException: attempt to access org.rosuda.REngine.REXPGenericVector as int
> eperson(economic)
[[1]]
[1] "主成份分析结果为"

[[2]]
Importance of components:
                          Comp.1    Comp.2    Comp.3     Comp.4    Comp.5      Comp.6 Comp.7
Standard deviation     2.1124391 1.1491308 0.9331551 0.50448791 0.2699638 0.137595030      0
Proportion of Variance 0.6374856 0.1886431 0.1243969 0.03635829 0.0104115 0.002704627      0
Cumulative Proportion  0.6374856 0.8261287 0.9505256 0.98688388 0.9972954 1.000000000      1

Loadings:
   Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7
x1 -0.446  0.127  0.138  0.431  0.599  0.241 -0.404
x2 -0.467                0.268                0.837
x3 -0.445  0.146 -0.243        -0.671  0.451 -0.260
x4 -0.377 -0.377  0.168 -0.763  0.236  0.223       
x5 -0.257 -0.297 -0.819         0.162 -0.382       
x6 -0.418  0.280  0.337 -0.137 -0.196 -0.730 -0.208
x7         0.808 -0.319 -0.375  0.265         0.150

[[3]]
[1] "综合得分和经济人口承载力为"

[[4]]
  年份      指数  承载人口
1 2006 2.2589596 178.29831
2 2007 1.9662692 155.19644
3 2008 1.6925468 133.59169
4 2009 0.2778931  21.93393
5 2010 0.8682956  68.53405
6 2011 1.8017713 142.21272
7 2012 2.9307567 231.32285

如何在java代码中显示与在RStudio中相同的结果?

如果您只想打印结果,请将结果存储为R中的变量,然后通过java将该变量的输出捕获为字符串:

c.eval("mod <- eperson(economic)");
String ret = c.eval("paste(capture.output(mod),collapse='\\n')").asString();
System.out.print(ret);

c.eval(“mod我对
R
一无所知,如果我说了一些愚蠢的话,很抱歉,但是:首先你将
xp
强制为
int
,然后你将它强制为一个字符串数组。这似乎不正确:它不能同时是两个。如果你用
xp.asStrings()替换
xp.asInteger()
).length
,结果是什么?哦,
asInteger()
asInteger()是一样的[0]
,所以这不是我想的那样。但无论如何,你的循环结构看起来像“打印数组的每个元素”,所以我的建议是一样的。你能先打印xp看看它的形式吗。从R异常来看,你访问的是向量,而不是向量中的字段,如[[I]]vs[I]xp是一种REXPGenericVector类型。在RStudio中运行“eperson(economic)”的结果就是xp。以我的方式,我希望将xp变成一个常规java数组,并可以打印它的值。