[英]Assigning an multi-dimensional array from Java to R through Rserve
I am using Java/R/Rserve for a project. 我正在使用Java / R / Rserve进行项目。 I am facing the problem of transferring a multi-dimensional array from Java into R workspace for calculation.
我面临的问题是将多维数组从Java转移到R工作空间进行计算。 So far, the
assign
method of the RConnection
object only allows the following to be passed: byte[], double[], int[], REXP, String,and String[]
. 到目前为止,
RConnection
对象的assign
方法只允许传递以下内容: byte[], double[], int[], REXP, String,and String[]
。
I sidestepped this by creating a loop in Java, and passed the variables individually. 我通过在Java中创建一个循环来回避这个问题,并单独传递变量。 Although this works, it looks ugly and inefficient.
虽然这有效但看起来很丑陋且效率低下。
RConnection c = new RConnection();
c.eval("x <- matrix(0,nrow=dimX[1],ncol=dimX[2])");
for (int i = 0; i < dimX[0]; i++){
c.assign("i",Integer.toString(i+1));
c.eval("i <- as.numeric(i)");
for (int j = 0; j < dimX[1]; j++){
c.assign("j",Integer.toString(j+1));
c.eval("j <- as.numeric(j)");
c.assign("tmp", Double.toString(XOBS[i][j]));
c.eval("x[i,j] <- as.numeric(tmp)");
}
}
The document for Rserve on http://www.rforge.net/Rserve/dist/JRclient/JavaDoc/org/rosuda/JRclient/REXP.html seems to be outdated, and the examples for Rserve are rather limited. 在http://www.rforge.net/Rserve/dist/JRclient/JavaDoc/org/rosuda/JRclient/REXP.html上的 Rserve文档似乎已经过时,Rserve的示例相当有限。 Could anyone give me a suggestion on how to improve on this code?
谁能给我一个关于如何改进这段代码的建议?
Thank you 谢谢
I found one solution and just made it a little bit more friendly, link on source also attached. 我找到了一个解决方案,只是让它更友好一点,链接源也附加。
Comments: it's ready-to-use utility method. 评论:它是随时可用的实用方法。 It based on JRI, which now is a part of rJava.
它基于JRI,现在是rJava的一部分。
Source: 资源:
http://www.lbgi.fr/wikili/index.php/JRI http://www.lbgi.fr/wikili/index.php/JRI
/**
* Creates and assigns a matrix object in R from 2D table of double
*
* @param rEngine the R instance used
* @param sourceArray the 2D table of double
* the matrix must have always the same column number on every row
* @param nameToAssignOn the R object name
* @return R matrix instance or null if R return an error
*/
public static REXP assignAsRMatrix(Rengine rEngine, double[][] sourceArray, String nameToAssignOn) {
if (sourceArray.length == 0) {
return null;
}
rEngine.assign(nameToAssignOn, sourceArray[0]);
REXP resultMatrix = rEngine.eval(nameToAssignOn + " <- matrix( " + nameToAssignOn + " ,nr=1)");
for (int i = 1; i < sourceArray.length; i++) {
rEngine.assign("temp", sourceArray[i]);
resultMatrix = rEngine.eval(nameToAssignOn + " <- rbind(" + nameToAssignOn + ",matrix(temp,nr=1))");
}
return resultMatrix;
}
供参考(在询问问题时可能尚未提供方法):
REXP REXP.createDoubleMatrix(double[][] arg);
what if you do something like this (altering row and line numbers for your needs)? 怎么做这样的事情(根据需要改变行号和行号)?
RConnection c = new RConnection();
double[][] test = { { 1.0D, 2.0D }, { 3.0D, 4.0D } };
c.assign("res", test[0]);
for (int i = 1; i < 2; i++) {
c.assign("tmp", test[i]);
c.eval("res<-rbind(res,tmp)");
}
REXP x = c.eval("sum(res)");
System.out.println(x.asString());
this returns 10, as expected, but, however, this 正如预期的那样,这将返回10,但是,这是
String s = c.eval("rowSums(res)").asString();
System.out.println(s);
doesnt printout what it suppose, it just returns 3
, maybe my Ubuntu-installed RServe is broken and can't print whatever is after space in result string 3 7
: 没有打印输出它假设的,它只返回
3
,也许我的Ubuntu安装的RServe坏了,无法打印结果字符串3 7
空格后的任何内容:
> rowSums(d)
c1 c2
3 7
and I cant find good examples too :( 我也找不到好的例子:(
You could: 你可以:
flatten the array into a vector of integer rows, such that 将数组展平为整数行的向量,这样
a11 a12 a11 a12
a21 a22 a21 a22
=> =>
flat_array = new int[] {a11, a12, a21, a22}
Assign that to a local variable eg: 将其分配给局部变量,例如:
rEngine.assign(".values", flat_array); rEngine.assign(“。values”,flat_array);
Call an R function that makes a matrix (or dataframe) in a global, like: 调用R函数,在全局中生成矩阵(或数据帧),如:
In R: 在R:
make.matrix <- function(nrows, ncols, values) {
value_mat <- matrix(values, nrow=nrows, ncol=ncols, byrow=TRUE)
temp.res <<- res
res
}
In Java: 在Java中:
rEngine.eval("make.matrix(2,2,.values)");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.