Copying to R a rectangular array from a Java class

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Copying to R a rectangular array from a Java class

Hurr
Bare test code: My simple Java test class source and R test code follow:
public class RJavTest {
  public static void main(String[]args) { RJavTest rJavTest=new RJavTest(); }
  public final static String conStg="testString";
  public final static double con0dbl=10000001;
  public final static double[]con1Arr=new double[] { 10001,10002,10003,10004,10005,10006 };
  public final static double[][]con2Arr=new double[][] { { 101,102,103,104 },{ 201,202,203,204 },{ 301,302,303,304 } };
  public final static String retConStg() { return(conStg); }
  public final static double retCon0dbl() { return(con0dbl); }
  public final static double[] retCon1Arr() { return(con1Arr); }
  public final static double[][] retCon2Arr() { return(con2Arr); }
}
library(rJava)
.jinit()
.jaddClassPath("C:/ad/j") # a directory on my disk
print(.jclassPath())
rJavaTst <- .jnew("RJavTest") # compilation of class code
connStg <- .jfield(rJavaTst,sig="S","conStg")
print(connStg)
connStgRet <- .jcall(rJavaTst,returnSig="S","retConStg")
print(connStgRet)
conn1Arr <- .jfield(rJavaTst,sig="[D","con1Arr")
print(conn1Arr)
print(conn1Arr[2])
conn1ArrRet <- .jcall(rJavaTst,returnSig="[D","retCon1Arr")
print(conn1ArrRet)
print(conn1ArrRet[2])
conn0dbl <- .jfield(rJavaTst,sig="D","con0dbl")
print(conn0dbl,digits=15)
# The above is just for education, question on rectangular is below
conn2Arr    <- .jfield(rJavaTst,sig="[[D","con2Arr")
conn2ArrRet <- .jcall(rJavaTst,returnSig="[[D","retCon2Arr")
# I can't identify any complaints so far
print(conn2Arr)
print(conn2ArrRet)

The results are:
> library(rJava)
> .jinit()
> .jaddClassPath("C:/ad/j") # a directory on my disk
> print(.jclassPath())
[1] "C:\\Users\\ENVY17\\Documents\\R\\win-library\\2.12\\rJava\\java"
[2] "C:\\ad\\j"                                                      
> rJavaTst <- .jnew("RJavTest") # compilation of class code
> connStg <- .jfield(rJavaTst,sig="S","conStg")
> print(connStg)
[1] "testString"
> connStgRet <- .jcall(rJavaTst,returnSig="S","retConStg")
> print(connStgRet)
[1] "testString"
> conn1Arr <- .jfield(rJavaTst,sig="[D","con1Arr")
> print(conn1Arr)
[1] 10001 10002 10003 10004 10005 10006
> print(conn1Arr[2])
[1] 10002
> conn1ArrRet <- .jcall(rJavaTst,returnSig="[D","retCon1Arr")
> print(conn1ArrRet)
[1] 10001 10002 10003 10004 10005 10006
> print(conn1ArrRet[2])
[1] 10002
> conn0dbl <- .jfield(rJavaTst,sig="D","con0dbl")
> print(conn0dbl,digits=15)
[1] 10000001
> # The above is just for education, question on rectangular is below
> conn2Arr    <- .jfield(rJavaTst,sig="[[D","con2Arr")
> conn2ArrRet <- .jcall(rJavaTst,returnSig="[[D","retCon2Arr")
> # I can't identify any complaints so far
> print(conn2Arr)
[[1]]
[1] "Java-Array-Object[D:[D@66848c"
[[2]]
[1] "Java-Array-Object[D:[D@8813f2"
[[3]]
[1] "Java-Array-Object[D:[D@1d58aae"
> print(conn2ArrRet)
[[1]]
[1] "Java-Array-Object[D:[D@66848c"
[[2]]
[1] "Java-Array-Object[D:[D@8813f2"
[[3]]
[1] "Java-Array-Object[D:[D@1d58aae"

>
But what meaning should get from these strange messages?

Reply | Threaded
Open this post in threaded view
|

Re: Copying to R a rectangular array from a Java class

Hurr
I discovered that a row of a rectangular array returns, but a function parameter is not sent to Java.
Appended bare test code: My simple Java test class source and R test code follow:
public class RJavTest {
  public static void main(String[]args) { RJavTest rJavTest=new RJavTest(); }
  public final static String conStg="testString";
  public final static double con0dbl=10000001;
  public final static double[]con1Arr=new double[] { 10001,10002,10003,10004,10005,10006 };
  public final static double[][]con2Arr=new double[][] { { 101,102,103,104 },{ 201,202,203,204 },{ 301,302,303,304 } };
  public final static String retConStg() { return(conStg); }
  public final static double retCon0dbl() { return(con0dbl); }
  public final static double[] retCon1Arr() { return(con1Arr); }
  public final static double[] retCon2Row0() { return(con2Arr[0]); }
  public final static double[] retCon2Row(int row) { return(con2Arr[row]); }
  public final static double[][] retCon2Arr() { return(con2Arr); }
}
library(rJava)
.jinit()
.jaddClassPath("C:/ad/j") # a directory on my disk
print(.jclassPath())
rJavaTst <- .jnew("RJavTest") # compiled java to class file
connStg <- .jfield(rJavaTst,sig="S","conStg")
print(connStg)
connStgRet <- .jcall(rJavaTst,returnSig="S","retConStg")
print(connStgRet)
conn1Arr <- .jfield(rJavaTst,sig="[D","con1Arr")
print(conn1Arr)
print(conn1Arr[2])
conn1ArrRet <- .jcall(rJavaTst,returnSig="[D","retCon1Arr")
print(conn1ArrRet)
print(conn1ArrRet[2])
conn0dbl <- .jfield(rJavaTst,sig="D","con0dbl")
print(conn0dbl,digits=15)
conn2Row0Ret <- .jcall(rJavaTst,returnSig="[D","retCon2Row0")
print(conn2Row0Ret)
print(conn2Row0Ret[2])
# The above is education, questions on rectangular and parameters are below
conn2Arr    <- .jfield(rJavaTst,sig="[[D","con2Arr")
conn2ArrRet <- .jcall(rJavaTst,returnSig="[[D","retCon2Arr")
# I can't identify any complaints so far
print(conn2Arr)
print(conn2ArrRet)
conn2RowRet <- .jcall(rJavaTst,returnSig="[D","retCon2Row",0)
print(conn2RowRet)
print(conn2RowRet[2])
# But what meaning should I get from these strange messages?

The results are:
> library(rJava)
> .jinit()
> .jaddClassPath("C:/ad/j") # a directory on my disk
> print(.jclassPath())
[1] "C:\\Users\\ENVY17\\Documents\\R\\win-library\\2.12\\rJava\\java"
[2] "C:\\ad\\j"                                                      
> rJavaTst <- .jnew("RJavTest") # compiled java to class file
> connStg <- .jfield(rJavaTst,sig="S","conStg")
> print(connStg)
[1] "testString"
> connStgRet <- .jcall(rJavaTst,returnSig="S","retConStg")
> print(connStgRet)
[1] "testString"
> conn1Arr <- .jfield(rJavaTst,sig="[D","con1Arr")
> print(conn1Arr)
[1] 10001 10002 10003 10004 10005 10006
> print(conn1Arr[2])
[1] 10002
> conn1ArrRet <- .jcall(rJavaTst,returnSig="[D","retCon1Arr")
> print(conn1ArrRet)
[1] 10001 10002 10003 10004 10005 10006
> print(conn1ArrRet[2])
[1] 10002
> conn0dbl <- .jfield(rJavaTst,sig="D","con0dbl")
> print(conn0dbl,digits=15)
[1] 10000001
> conn2Row0Ret <- .jcall(rJavaTst,returnSig="[D","retCon2Row0")
> print(conn2Row0Ret)
[1] 101 102 103 104
> print(conn2Row0Ret[2])
[1] 102
> # The above is education, questions on rectangular and parameters are below
> conn2Arr    <- .jfield(rJavaTst,sig="[[D","con2Arr")
> conn2ArrRet <- .jcall(rJavaTst,returnSig="[[D","retCon2Arr")
> # I can't identify any complaints so far
> print(conn2Arr)
[[1]]
[1] "Java-Array-Object[D:[D@66848c"
[[2]]
[1] "Java-Array-Object[D:[D@8813f2"
[[3]]
[1] "Java-Array-Object[D:[D@1d58aae"
> print(conn2ArrRet)
[[1]]
[1] "Java-Array-Object[D:[D@66848c"
[[2]]
[1] "Java-Array-Object[D:[D@8813f2"
[[3]]
[1] "Java-Array-Object[D:[D@1d58aae"
> conn2RowRet <- .jcall(rJavaTst,returnSig="[D","retCon2Row",0)
Error in .jcall(rJavaTst, returnSig = "[D", "retCon2Row", 0) :
  method retCon2Row with signature (D)[D not found
> print(conn2RowRet)
Error in print(conn2RowRet) : object 'conn2RowRet' not found
> print(conn2RowRet[2])
Error in print(conn2RowRet[2]) : object 'conn2RowRet' not found
> # But what meaning should I get from these strange messages?
>
Reply | Threaded
Open this post in threaded view
|

Re: Copying to R a rectangular array from a Java class

Hurr
In reply to this post by Hurr
I am happy to report that the author and maintainer of rJava informed me
that the 2-dim array in java needs sapply and .jevalArray as follows:

> conn2Arr    <- sapply(.jfield(rJavaTst,sig="[[D","con2Arr"),.jevalArray)
> conn2ArrRet <- sapply(.jcall(rJavaTst,returnSig="[[D","retCon2Arr"),.jevalArray)
> # I can't identify any complaints so far
> print(conn2Arr)
     [,1] [,2] [,3]
[1,]  101  201  301
[2,]  102  202  302
[3,]  103  203  303
[4,]  104  204  304
> print(conn2ArrRet)
     [,1] [,2] [,3]
[1,]  101  201  301
[2,]  102  202  302
[3,]  103  203  303
[4,]  104  204  304
 I know there are a few out there interested since I see I got a few views.
But, I don't know the solution to the parameter-passing problem yet.
Reply | Threaded
Open this post in threaded view
|

Re: Copying to R a rectangular array from a Java class

Sanketh
Hi, can you please tel me how to retrieve String two dimensional array as like sapply?