I am using Java/R/Rserve for a project. I am facing the problem of transferring a multi-dimensional array from Java into R workspace for calculation. So far, the assign method of the RConnection object only allows the following to be passed: byte[], double[], int[], REXP, String,and String[].
I sidestepped this by creating a loop in Java, and passed the variables individually. 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. Could anyone give me a suggestion on how to improve on this code?
Thank you
evalwithrbindin R, another way - is to dump all your Java data into the file and doread.tablein R.