Using Java methods in R

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Using Java methods in R

Hurr
I just now registered in R forum.
I use Windows because of past history of using windows.
I have been writing and using programs in java for about a decade.
I work with unequal-interval-time-series data files, 1st 5 lines like:
Title Line
CnYrMoDaHrMnScDCMQ,ColSerTit1,ColSerTit2,ColSerTit3,ColSerTit4,
201104212359599999,Ser1Datum1,Ser2Datum1,Ser3Datum1,Ser4Datum1,
201104220059428739,Ser1Datum2,Ser2Datum2,Ser3Datum2,Ser4Datum2,
201104220103369291,Ser1Datum3,Ser2Datum3,Ser3Datum3,Ser4Datum3,
where e.g.Ser4Datum3 is a number, and calendartime unit can be
anywhere from Da down to Q (Quadiseconds), a lot stops at Mn.
Time is same unit throughout any one file.

Getting to the point: I have all of the calendar-linear conversion
and file reading subroutines as Java methods and would like to
use them in R. I am not very knowledgeable yet about R.
I need instructions about how to set up and use java in R on Windows.
Can anyone help me with setup instructions then starting with a
simple Java function (method) call. One problem with Java is that a
function returns only one item.
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
No answer to my post,
so let's try a simpler question. Am I doing this correctly?
I have the RGui with R Console on the screen.
On rhe top pullDowns, Packages > Install Packages > USA(IA)> rJava
> library(rJava)
> .jinit()
> qsLin <- .jnew("C:/ad/j/CalqsLin")
Error in .jnew("C:/ad/j/CalqsLin") :
  java.lang.NoClassDefFoundError: C:/ad/j/CalqsLin

So I got this error which means I don't understand very much.
I go to C:/ad/j and get
C:\ad\j>dir CalqsLin.class
 Volume in drive C has no label.
 Volume Serial Number is 9A35-67A2
 Directory of C:\ad\j
04/23/2011  07:11 AM            14,651 CalqsLin.class
               1 File(s)         14,651 bytes
               0 Dir(s)  104,257,716,224 bytes free

Just to show my intentions,
I had next wanted to call this java function method:
linTimOfCalqsStgIsLev("201104052359599999",-4) using:
> dblTim <- .jcall(qsLin,"D","linTimOfCalqsStgIsLev","201104052359599999","-4")
but that will probably also be wrong?
Obviously I don't understand.
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Gabor Grothendieck
On Sat, Apr 23, 2011 at 8:32 AM, hill0093 <[hidden email]> wrote:

> No answer to my post,
> so let's try a simpler question. Am I doing this correctly?
> I have the RGui with R Console on the screen.
> On rhe top pullDowns, Packages > Install Packages > USA(IA)> rJava
>> library(rJava)
>> .jinit()
>> qsLin <- .jnew("C:/ad/j/CalqsLin")
> Error in .jnew("C:/ad/j/CalqsLin") :
>  java.lang.NoClassDefFoundError: C:/ad/j/CalqsLin
>
> So I got this error which means I don't understand very much.
> I go to C:/ad/j and get
> C:\ad\j>dir CalqsLin.class
>  Volume in drive C has no label.
>  Volume Serial Number is 9A35-67A2
>  Directory of C:\ad\j
> 04/23/2011  07:11 AM            14,651 CalqsLin.class
>               1 File(s)         14,651 bytes
>               0 Dir(s)  104,257,716,224 bytes free
>
> Just to show my intentions,
> I had next wanted to call this java function method:
> linTimOfCalqsStgIsLev("201104052359599999",-4) using:
>> dblTim <-
>> .jcall(qsLin,"D","linTimOfCalqsStgIsLev","201104052359599999","-4")
> but that will probably also be wrong?
> Obviously I don't understand.

Have a look at the source of other packages that use rJava.  Such
packages are listed at the bottom of this page:
http://cran.r-project.org/web/packages/rJava/index.html

Also note that there is a separate list for R and Java:
http://mailman.rz.uni-augsburg.de/mailman/listinfo/stats-rosuda-devel

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
That's more than I can understand.
I just need help from someone who knows.
Should be a simple answer from that person.
lcn
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

lcn
Well, the call to Java methods should be really simple in rJava.
What you had tried is actually correct, initialize jvm, create an object, then call method.

For the error you met, maybe you should check .jaddClassPath.


At 2011-04-23 20:58:22£¬hill0093 <[hidden email]> wrote:

>That's more than I can understand.
>I just need help from someone who knows.
>Should be a simple answer from that person.
>
>
>--
>View this message in context: http://r.789695.n4.nabble.com/Using-Java-methods-in-R-tp3469299p3469882.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
In reply to this post by Hurr
Since it cannot find a java class that is on my disk,
is there a place I have to put it into first?
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Shekhar-2
Hi Hill,
I just finished interfacing the C++ with R, i.e. c++ functions from R
and vice versa. Next thing in pipe line is Java and python.
I just wanted to share some of the things which i think might be
useful for you. Reason being whether its a C++, java or any other
compiled language the interface has to be generic in R. That means
mode of interfacing might be different(using different packages Rcpp
for c++, rJava for Java) but the underlying mechanism remains the
same.

For C++ you will be creating a dll ( windows) and .so( *nix) and for
jave you will be using class.
Now for using c++/java functions with in R or vice versa, you first
should check whether the functions are properly loaded in the R symbol
table or not?

If you are unable to call the java functions in R, then probably you
are unable to load the class into the R environment.

A few steps which i have followed in C++ ( i think must be more or
less same conceptually) could be useful for you:

1) I have a c++ file, i compile it using the command R CMD SHLIB
filename.cpp. On successful completion it creates the library. if you
are getting compilation error then you need to check whether the
header files are in proper path or not.

2) Then for using this function is first use to load the library using
the function dyn.load("library path")

3) If the loading is successful i check whether the required function
is properly loaded into the R symbol table or not by using the
function is.loaded("function name")

4) if it returns true, then i call the function by using .C or .Call
function provided by R.
For more information you can see http://groups.google.com/group/brumail/browse_thread/thread/a278dcbb6a8a439a
.

Hope it helps you in someway.

Regards,
Som Shekhar

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Mike Marchywka
In reply to this post by Hurr















----------------------------------------

> Date: Sat, 23 Apr 2011 05:32:59 -0700
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [R] Using Java methods in R
>
> No answer to my post,
> so let's try a simpler question. Am I doing this correctly?
> I have the RGui with R Console on the screen.
> On rhe top pullDowns, Packages > Install Packages > USA(IA)> rJava
> > library(rJava)
> > .jinit()
> > qsLin <- .jnew("C:/ad/j/CalqsLin")
> Error in .jnew("C:/ad/j/CalqsLin") :
> java.lang.NoClassDefFoundError: C:/ad/j/CalqsLin

i haven't used rjava yet, I think I installed it on linux
for testing but no real usage, but this message
appears to come from jvm probably because you
specified an aboslute path. You can try this from
command line,




$ ls ../geo/dayte*
../geo/dayte.class

mmarchywka@phlulap01 /cygdrive/c/d/phluant/duphus
$ java ../geo/dayte
Exception in thread "main" java.lang.NoClassDefFoundError: ///geo/dayte
Caused by: java.lang.ClassNotFoundException: ...geo.dayte
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: ../geo/dayte.  Program will exit.


you want to search for the classpath and set that up then
refernce the class name not the path ( the class loader will follow
the path looking for it ).
Not sure how you do that in rjava under 'dohs but
if you search for that term is should be apparent.
And of course 'dohs and pdf aren't always friendly
for automated work so get something like cygwin and
reduce pdf's to text etc.






>
> So I got this error which means I don't understand very much.
> I go to C:/ad/j and get
> C:\ad\j>dir CalqsLin.class
> Volume in drive C has no label.
> Volume Serial Number is 9A35-67A2
> Directory of C:\ad\j
> 04/23/2011 07:11 AM 14,651 CalqsLin.class
> 1 File(s) 14,651 bytes
> 0 Dir(s) 104,257,716,224 bytes free
>
> Just to show my intentions,
> I had next wanted to call this java function method:
> linTimOfCalqsStgIsLev("201104052359599999",-4) using:
> > dblTim <-
> > .jcall(qsLin,"D","linTimOfCalqsStgIsLev","201104052359599999","-4")
> but that will probably also be wrong?
> Obviously I don't understand.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Using-Java-methods-in-R-tp3469299p3469848.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
     
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Mike Marchywka













----------------------------------------

> From: [hidden email]
> To: [hidden email]; [hidden email]
> Date: Sat, 23 Apr 2011 15:12:30 -0400
> Subject: Re: [R] Using Java methods in R
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> ----------------------------------------
> > Date: Sat, 23 Apr 2011 05:32:59 -0700
> > From: [hidden email]
> > To: [hidden email]
> > Subject: Re: [R] Using Java methods in R
> >
> > No answer to my post,
> > so let's try a simpler question. Am I doing this correctly?
> > I have the RGui with R Console on the screen.
> > On rhe top pullDowns, Packages > Install Packages > USA(IA)> rJava
> > > library(rJava)
> > > .jinit()
> > > qsLin <- .jnew("C:/ad/j/CalqsLin")
> > Error in .jnew("C:/ad/j/CalqsLin") :
> > java.lang.NoClassDefFoundError: C:/ad/j/CalqsLin
>
> i haven't used rjava yet, I think I installed it on linux
> for testing but no real usage, but this message
> appears to come from jvm probably because you
> specified an aboslute path. You can try this from
> command line,
>
>
>
>
> $ ls ../geo/dayte*
> ../geo/dayte.class
>
> mmarchywka@phlulap01 /cygdrive/c/d/phluant/duphus
> $ java ../geo/dayte
> Exception in thread "main" java.lang.NoClassDefFoundError: ///geo/dayte
> Caused by: java.lang.ClassNotFoundException: ...geo.dayte
>         at java.net.URLClassLoader$1.run(Unknown Source)
>         at java.security.AccessController.doPrivileged(Native Method)
>         at java.net.URLClassLoader.findClass(Unknown Source)
>         at java.lang.ClassLoader.loadClass(Unknown Source)
>         at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
>         at java.lang.ClassLoader.loadClass(Unknown Source)
> Could not find the main class: ../geo/dayte.  Program will exit.
>
>
> you want to search for the classpath and set that up then
> refernce the class name not the path ( the class loader will follow
> the path looking for it ).
> Not sure how you do that in rjava under 'dohs but
> if you search for that term is should be apparent.
> And of course 'dohs and pdf aren't always friendly
> for automated work so get something like cygwin and
> reduce pdf's to text etc.
>
>

It appears that by default the current directory is on the
classpath. I must have installed this on 'dohs before
and if I copy the class file into R directory it
can find it,




> library("rJava")
> .jinit()
> .jnew("foo")
Error in .jnew("foo") : java.lang.NoClassDefFoundError: foo
> .jnew("dayte")
[1] "Java-Object{dayte@1de3f2d}"
>

that's unlikely to fix all your problems, you want to set the classpath
but if you know the term and can load cygwin you should be able
to find the way to set that up.












>
>
>
>
> >
> > So I got this error which means I don't understand very much.
> > I go to C:/ad/j and get
> > C:\ad\j>dir CalqsLin.class
> > Volume in drive C has no label.
> > Volume Serial Number is 9A35-67A2
> > Directory of C:\ad\j
> > 04/23/2011 07:11 AM 14,651 CalqsLin.class
> > 1 File(s) 14,651 bytes
> > 0 Dir(s) 104,257,716,224 bytes free
> >
> > Just to show my intentions,
> > I had next wanted to call this java function method:
> > linTimOfCalqsStgIsLev("201104052359599999",-4) using:
> > > dblTim <-
> > > .jcall(qsLin,"D","linTimOfCalqsStgIsLev","201104052359599999","-4")
> > but that will probably also be wrong?
> > Obviously I don't understand.
> >
> >
> > --
> > View this message in context: http://r.789695.n4.nabble.com/Using-Java-methods-in-R-tp3469299p3469848.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
     
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
In reply to this post by Shekhar-2
Hi Shekhar, Thank you. Right path.
Sounds good. Please help me when you discover Java interface.
Firstly, I don't know what the R symbol table is or how to do
the checking you suggest. I am really naive about R.
I want to be able to get my data in there so I  can learn to use
it for my purposes.
Hill
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
In reply to this post by lcn
Thanks Icn, for suggesting .jaddClassPath
I finally had time to play around and discover what you meant.
So I am one step farther than the report in the second post above:
I have the RGui with R Console on the screen.
On the top pullDowns, Packages > Install Packages > USA(IA)> rJava
> library(rJava)
> .jinit()
> ## > ?rJava::.jaddClassPath
> .jaddClassPath("C:/ad/j")  ##or > .jaddClassPath("C:\\ad\\j")
> print(.jclassPath())
[1] "C:\\Users\\ENVY17\\Documents\\R\\win-library\\2.12\\rJava\\java"
[2] "C:\\ad\\j"                                                      
> qsLin <- .jnew("CalqsLin")
> dblTim <- .jcall(qsLin,returnSig="D","linTimOfCalqsStgIsLev","201104052359599999","-4")
Error in .jcall(qsLin, returnSig = "D", "linTimOfCalqsStgIsLev", "201104052359599999",  :
  method linTimOfCalqsStgIsLev with signature (Ljava/lang/String;Ljava/lang/String;)D not found
>

What's wrong?
Just to show my intentions,
I had next wanted to call this java function method:
linTimOfCalqsStgIsLev("201104052359599999",-4)
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
So I am one step farther than last time, so I am happy:
I have the RGui with R Console on the screen.
On the top pullDowns, Packages > Install Packages > USA(IA)> rJava
> library(rJava)
> .jinit()
> .jaddClassPath("C:/ad/j")
> print(.jclassPath())
[1] "C:\\Users\\ENVY17\\Documents\\R\\win-library\\2.12\\rJava\\java"
[2] "C:\\ad\\j"                                                      
> qsLin <- .jnew("CalqsLin")
> calStg <- "201104242359599999"
> print(calStg)
[1] "201104242359599999"
> dblTim <- .jcall(qsLin,returnSig="D","linTimOfCalqsStgIsLev",calStg,as.integer(-4))
> print(dblTim,digits=20)
[1] 634709087999999
> calStg <- .jcall(qsLin,returnSig="S","calqsStgOfLinTimIsLev",dblTim,as.integer(-4))
> print(calStg)
[1] "201104242359599999"
> dblTim <- .jcall(qsLin,returnSig="D","linTimOfCalqsStgIsLev",calStg,as.integer(-4))
> print(dblTim,digits=20)
[1] 634709087999999
>
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
I added this R code to the bottom of the previous code and it doesn't work

> ar34Ret <- .jcall(qsLin,returnSig="[[D","arReturnTEST")
> print(ar34Ret,digits=20)
[[1]]
[1] "Java-Array-Object[D:[D@8813f2"

[[2]]
[1] "Java-Array-Object[D:[D@1d58aae"

[[3]]
[1] "Java-Array-Object[D:[D@83cc67"

> for(i in 1:3) for(j in 1:4) print(ar34Ret(i,j),digits=15)
Error in print(ar34Ret(i, j), digits = 15) :
  could not find function "ar34Ret"
>

The Java code is:
public final static double[][] arReturnTEST() {
  double[][]retArr=new double[3][4]; for(int i=0;i<3;i++)for(int j=0;j<4;j++)retArr[i][j]=i*1000+j; return(retArr);
}
R doesn't know how to print the array in the next to final line,
and thinks the array is a function in the final line.

Any suggestions?
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
So the first few posts show that I found out how to
get Java functions to return type double numbers to R.
The arrays are still a problem.

Here is another of my attempts to understand how to get java arrays into R.
The Java code in class CalqsLin for an array
of constants named conArr and for a function returning an array is:
public static double[][]conArr=new double[][] { { 10001,10002,10003,10004 },{ 20001,20002,20003,20004 },{ 30001,30002,30003,30004 } };
public final static double[][] arReturnTEST() {
  double[][]retArr=new double[3][4]; for(int i=0;i<3;i++)for(int j=0;j<4;j++)retArr[i][j]=i*1000+j; return(retArr);
}

I evidently do not know how to retrieve them in R, even with .jevalArray():
> arj34Ret <- .jcall(qsLin,returnSig="[[D","arReturnTEST")
> ar34Ret <- .jevalArray(arj34Ret)
Error in .jevalArray(arj34Ret) :
  object is not a Java object reference (jobjRef/jarrayRef).
> connArr <- .jevalArray(qsLin.conArr)
Error in .jevalArray(qsLin.conArr) : object 'qsLin.conArr' not found

I need help from someone who calls Java from R.
The whole session was:
> library(rJava)
> .jinit()
> .jaddClassPath("C:/ad/j")
> print(.jclassPath())
[1] "C:\\Users\\ENVY17\\Documents\\R\\win-library\\2.12\\rJava\\java"
[2] "C:\\ad\\j"                                                      
> qsLin <- .jnew("CalqsLin")
> calStg <- "201104242359599999"
> print(calStg)
[1] "201104242359599999"
> dblTim <- .jcall(qsLin,returnSig="D","linTimOfCalqsStgIsLev",calStg,as.integer(-4))
> print(dblTim,digits=20)
[1] 634709087999999
> calStg <- .jcall(qsLin,returnSig="S","calqsStgOfLinTimIsLev",dblTim,as.integer(-4))
> print(calStg)
[1] "201104242359599999"
> dblTim <- .jcall(qsLin,returnSig="D","linTimOfCalqsStgIsLev",calStg,as.integer(-4))
> print(dblTim,digits=20)
[1] 634709087999999
> arj34Ret <- .jcall(qsLin,returnSig="[[D","arReturnTEST")
> ar34Ret <- .jevalArray(arj34Ret)
Error in .jevalArray(arj34Ret) :
  object is not a Java object reference (jobjRef/jarrayRef).
> connArr <- .jevalArray(qsLin.conArr)
Error in .jevalArray(qsLin.conArr) : object 'qsLin.conArr' not found
>
lcn
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

lcn
In reply to this post by Hurr
> for(i in 1:3) for(j in 1:4) print(ar34Ret(i,j),digits=15)
>Error in print(ar34Ret(i, j), digits = 15) :
>  could not find function "ar34Ret"
The way you used

ar34Ret(i, j)
was actually wrong. It should be ar34Ret[i, j], if nothing else wrong.

At 2011-04-25 08:06:51£¬hill0093 <[hidden email]> wrote:

>I added this R code to the bottom of the previous code and it doesn't work
>
>> ar34Ret <- .jcall(qsLin,returnSig="[[D","arReturnTEST")
>> print(ar34Ret,digits=20)
>[[1]]
>[1] "Java-Array-Object[D:[D@8813f2"
>
>[[2]]
>[1] "Java-Array-Object[D:[D@1d58aae"
>
>[[3]]
>[1] "Java-Array-Object[D:[D@83cc67"
>
>> for(i in 1:3) for(j in 1:4) print(ar34Ret(i,j),digits=15)
>Error in print(ar34Ret(i, j), digits = 15) :
>  could not find function "ar34Ret"
>>
>
>The Java code is:
>public final static double[][] arReturnTEST() {
>  double[][]retArr=new double[3][4]; for(int i=0;i<3;i++)for(int
>j=0;j<4;j++)retArr[i][j]=i*1000+j; return(retArr);
>}
>R doesn't know how to print the array in the next to final line,
>and thinks the array is a function in the final line.
>
>Any suggestions?
>
>
>--
>View this message in context: http://r.789695.n4.nabble.com/Using-Java-methods-in-R-tp3469299p3472308.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
lcn
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

lcn
In reply to this post by Hurr
you should take a look at the doc of .jarray by input "?.jarray" in the console and see related examples by "example(.jarray)" in the console.

This is what the doc says, and your issues might be related to it:
     '.jevalArray' currently supports only a subset of all possible
     array types. Recursive arrays are handled by returning a list of
     references which can then be evaluated separately.

At 2011-04-25 20:27:24£¬hill0093 <[hidden email]> wrote:

>So the first few posts show that I found out how to
>get Java functions to return type double numbers to R.
>The arrays are still a problem.
>
>Here is another of my attempts to understand how to get java arrays into R.
>The Java code in class CalqsLin for an array
>of constants named conArr and for a function returning an array is:
>public static double[][]conArr=new double[][] { { 10001,10002,10003,10004
>},{ 20001,20002,20003,20004 },{ 30001,30002,30003,30004 } };
>public final static double[][] arReturnTEST() {
>  double[][]retArr=new double[3][4]; for(int i=0;i<3;i++)for(int
>j=0;j<4;j++)retArr[i][j]=i*1000+j; return(retArr);
>}
>
>I evidently do not know how to retrieve them in R, even with .jevalArray():
>> arj34Ret <- .jcall(qsLin,returnSig="[[D","arReturnTEST")
>> ar34Ret <- .jevalArray(arj34Ret)
>Error in .jevalArray(arj34Ret) :
>  object is not a Java object reference (jobjRef/jarrayRef).
>> connArr <- .jevalArray(qsLin.conArr)
>Error in .jevalArray(qsLin.conArr) : object 'qsLin.conArr' not found
>
>I need help from someone who calls Java from R.
>The whole session was:
>> library(rJava)
>> .jinit()
>> .jaddClassPath("C:/ad/j")
>> print(.jclassPath())
>[1] "C:\\Users\\ENVY17\\Documents\\R\\win-library\\2.12\\rJava\\java"
>[2] "C:\\ad\\j"                                                      
>> qsLin <- .jnew("CalqsLin")
>> calStg <- "201104242359599999"
>> print(calStg)
>[1] "201104242359599999"
>> dblTim <-
>> .jcall(qsLin,returnSig="D","linTimOfCalqsStgIsLev",calStg,as.integer(-4))
>> print(dblTim,digits=20)
>[1] 634709087999999
>> calStg <-
>> .jcall(qsLin,returnSig="S","calqsStgOfLinTimIsLev",dblTim,as.integer(-4))
>> print(calStg)
>[1] "201104242359599999"
>> dblTim <-
>> .jcall(qsLin,returnSig="D","linTimOfCalqsStgIsLev",calStg,as.integer(-4))
>> print(dblTim,digits=20)
>[1] 634709087999999
>> arj34Ret <- .jcall(qsLin,returnSig="[[D","arReturnTEST")
>> ar34Ret <- .jevalArray(arj34Ret)
>Error in .jevalArray(arj34Ret) :
>  object is not a Java object reference (jobjRef/jarrayRef).
>> connArr <- .jevalArray(qsLin.conArr)
>Error in .jevalArray(qsLin.conArr) : object 'qsLin.conArr' not found
>>
>
>
>--
>View this message in context: http://r.789695.n4.nabble.com/Using-Java-methods-in-R-tp3469299p3473023.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
Thanks Icn for pointing that out, but I don't understand it.
My use of .jcall to return a type double scalar or String worked.
My use of .jfield to get a one dimensional static array worked.
My use of .jfield to get a static scalar constant did not work.
My use of .jfield to get a two dimensional static constant array did not work.
I don't understand the remainder of my rjava trial.

The C:/ad/j/CalqsLin java code is:
  public final static double con0dbl=10001;
  public final static double[]con1Arr=new double[] { 10001,10002,10003,10004,10005,10006 };
  public final static double[][]con2Arr=new double[][] { { 10001,10002,10003,10004 },{ 20001,20002,20003,20004 },{ 30001,30002,30003,30004 } };
  public final static double[][] arReturnTEST() {
    double[][]retArr=new double[3][4]; for(int i=0;i<3;i++)for(int j=0;j<4;j++)retArr[i][j]=i*1000+j; return(retArr);
  }
I have the RGui with R Console on the screen; here is the trial:
On the top pullDowns: Packages > Install Packages > USA(IA)> rJava
> library(rJava)
> .jinit()
[1] 0
> .jaddClassPath("C:/ad/j")
> print(.jclassPath())
[1] "C:\\Users\\ENVY17\\Documents\\R\\win-library\\2.12\\rJava\\java"
[2] "C:\\ad\\j"                                                      
> qsLin <- .jnew("CalqsLin")
> calStg <- "201104242359599999"
> print(calStg)
[1] "201104242359599999"
> dblTim <- .jcall(qsLin,returnSig="D","linTimOfCalqsStgIsLev",calStg,as.integer(-4))
> print(dblTim,digits=20)
[1] 634709087999999
> calStg <- .jcall(qsLin,returnSig="S","calqsStgOfLinTimIsLev",dblTim,as.integer(-4))
> print(calStg)
[1] "201104242359599999"
> conn1Arr <- .jfield(qsLin,sig="[D","con1Arr")
> print(conn1Arr)
[1] 10001 10002 10003 10004 10005 10006
> conn0dbl <- .jfield(qsLin,sig="D","con0dbl")
Error in .jfield(qsLin, sig = "D", "con0dbl") :
  RgetField: field con0dbl not found
> arj34Ret <- .jcall(qsLin,returnSig="[[D","arReturnTEST")
> print(arj34Ret[2][3],digits=15)
[[1]]
NULL
> print(arj34Ret,digits=15)
[[1]]
[1] "Java-Array-Object[D:[D@1d58aae"
[[2]]
[1] "Java-Array-Object[D:[D@83cc67"
[[3]]
[1] "Java-Array-Object[D:[D@e09713"
> ar34Ret <- .jevalArray(arj34Ret)
Error in .jevalArray(arj34Ret) :
  object is not a Java object reference (jobjRef/jarrayRef).
> print(ar34Ret[2][3],digits=15)
Error in print(ar34Ret[2][3], digits = 15) : object 'ar34Ret' not found
> print(ar34Ret,digits=20)
Error in print(ar34Ret, digits = 20) : object 'ar34Ret' not found
>






lcn
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

lcn
I guess your failure of getting two dimensional array may be related to this :
http://www.rforge.net/rJava/news.html
"0.9-0   (under development)
    o   fixes issues introduced by several new features in the late
        0.8 series. Most imporantly .jarray() and .jevalArray() behave
        as intended (and as implemented in previous versions). The
        same applies to .jcall() behavior with respect to arrays and
        its evalArray arument. The defaults for some new arguments
        have been changed to reflect the original behavior.
   o   .jevalArray() has an additional argument `simplify' which allows
        multi-dimensional arrays to be converted to native R
        types. Use with care - it may convert more recursive types in
        the future so it should be used preferrably where you have
        control over the types converted.
   o   fixed a bug in .jevalArray that was not simplifying string
        vectors correctly when the array type was not specified
        (.jclass returns dot notation whereas signatures use JNI
        notation so a conversion was necessary)"

So for multi-dimensional array, the.jevalArrayfunction has not been fully implemented in rJava release.

As for the static double field con0dbl, it's pretty weild since it works well when I tested your code.

One more notice, ar34Ret[2][3] should be "ar34Ret[2,3]" in R.

At 2011-04-27 04:01:00£¬hill0093 <[hidden email]> wrote:

>Thanks Icn for pointing that out, but I don't understand it.
>My use of .jcall to return a type double scalar or String worked.
>My use of .jfield to get a one dimensional static array worked.
>My use of .jfield to get a static scalar constant did not work.
>My use of .jfield to get a two dimensional static constant array did not
>work.
>I don't understand the remainder of my rjava trial.
>
>The C:/ad/j/CalqsLin java code is:
>  public final static double con0dbl=10001;
>  public final static double[]con1Arr=new double[] {
>10001,10002,10003,10004,10005,10006 };
>  public final static double[][]con2Arr=new double[][] { {
>10001,10002,10003,10004 },{ 20001,20002,20003,20004 },{
>30001,30002,30003,30004 } };
>  public final static double[][] arReturnTEST() {
>    double[][]retArr=new double[3][4]; for(int i=0;i<3;i++)for(int
>j=0;j<4;j++)retArr[i][j]=i*1000+j; return(retArr);
>  }
>I have the RGui with R Console on the screen; here is the trial:
>On the top pullDowns: Packages > Install Packages > USA(IA)> rJava
>> library(rJava)
>> .jinit()
>[1] 0
>> .jaddClassPath("C:/ad/j")
>> print(.jclassPath())
>[1] "C:\\Users\\ENVY17\\Documents\\R\\win-library\\2.12\\rJava\\java"
>[2] "C:\\ad\\j"                                                      
>> qsLin <- .jnew("CalqsLin")
>> calStg <- "201104242359599999"
>> print(calStg)
>[1] "201104242359599999"
>> dblTim <-
>> .jcall(qsLin,returnSig="D","linTimOfCalqsStgIsLev",calStg,as.integer(-4))
>> print(dblTim,digits=20)
>[1] 634709087999999
>> calStg <-
>> .jcall(qsLin,returnSig="S","calqsStgOfLinTimIsLev",dblTim,as.integer(-4))
>> print(calStg)
>[1] "201104242359599999"
>> conn1Arr <- .jfield(qsLin,sig="[D","con1Arr")
>> print(conn1Arr)
>[1] 10001 10002 10003 10004 10005 10006
>> conn0dbl <- .jfield(qsLin,sig="D","con0dbl")
>Error in .jfield(qsLin, sig = "D", "con0dbl") :
>  RgetField: field con0dbl not found
>> arj34Ret <- .jcall(qsLin,returnSig="[[D","arReturnTEST")
>> print(arj34Ret[2][3],digits=15)
>[[1]]
>NULL
>> print(arj34Ret,digits=15)
>[[1]]
>[1] "Java-Array-Object[D:[D@1d58aae"
>[[2]]
>[1] "Java-Array-Object[D:[D@83cc67"
>[[3]]
>[1] "Java-Array-Object[D:[D@e09713"
>> ar34Ret <- .jevalArray(arj34Ret)
>Error in .jevalArray(arj34Ret) :
>  object is not a Java object reference (jobjRef/jarrayRef).
>> print(ar34Ret[2][3],digits=15)
>Error in print(ar34Ret[2][3], digits = 15) : object 'ar34Ret' not found
>> print(ar34Ret,digits=20)
>Error in print(ar34Ret, digits = 20) : object 'ar34Ret' not found
>>
>
>
>
>
>
>
>
>
>--
>View this message in context: http://r.789695.n4.nabble.com/Using-Java-methods-in-R-tp3469299p3476371.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
Thanks Icn for the lookup.
I appreciate your skill.
The static double field con0dbl started working for me too.
I was surprised, and checked my code carefully.
I think they corrected that in rJava.
I download and install rJava each time I use R.
Hill
Reply | Threaded
Open this post in threaded view
|

Re: Using Java methods in R

Hurr
I don't know who to contact in the management of this R-forum,
and there are a few things I cannot figure out.

As I understand it, to participate and learn on the R-forum,
I must receive all the emails even concerning topics I have no
interest in currently. It clogs up my email, takes a long
time to delete, and is hard to be selective enough to not
delete some of my other important email. It is nice to
receive email on my posts and possibly even those others that
I have responded to. Do I understand this correctly?

Another item is that rJava has existed and has been promoted for
a long time, and yet it doesn't seem to work for even some
simple things like getting exactly-rectangular arrays from
Java classes. Of course it could be that I just don't know
how to use it, but people on the forum don't seem to know
either. Maybe the people that know don't use the forum because
of all the email.

Or maybe there are not enough people that are writing rJava.
And I am not talented enough to do so.

12