[R] An example of using rJava

2006-11-29 Thread Jason Liao
I have received a few private emails asking for some simple
demonstration of calling Java code from R using the rJava package
(which can be installed directly inside R). Here is one example for
convolving two vectors (an example in the R manual about linking C with
R).

First write a Java program


public class my_convolve
{   
 public static double[] convolve(double[] a, double[] b)
 {
 int n1 = a.length;
 int n2 = b.length;
 int n3 = n1+n1-1;
 
 double[] c = new double[n3];
 for(int i=0; in1; i++)
for(int j=0; jn2; j++) c[i+j] += a[i]*b[j];

 return c;
  }   
} 
Compile the Java code.

now inside R

library(rJava) #load the rJava library
.jinit(classpath=c:/liao_all/importance_sampling/java,
parameters=-Xmx512m)
#the above is to load the Java virtual machine,
c:/liao_all/importance_sampling/java is where the java code is.

x = runif(1000)
y = runif(1000)
#the above are two vectors to convolve

#now you can call the Java method as
z=.jcall(my_convolve, [D, convolve, x,y)
#z stores the result

Hope this helps.





Jason Liao, http://www.geocities.com/jg_liao
Associate Professor of Biostatistics
Drexel University School of Public Health
245 N. 15th Street, Mail Stop 660
Philadelphia, PA 19102-1192
phone 215-762-3934


 

Cheap talk?

__
R-help@stat.math.ethz.ch 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.


Re: [R] An example of using rJava

2006-11-29 Thread Rajarshi Guha
On Wed, 2006-11-29 at 08:48 -0800, Jason Liao wrote:
 I have received a few private emails asking for some simple
 demonstration of calling Java code from R using the rJava package
 (which can be installed directly inside R).

One could also examine the sources of the rcdk package which provides
access to the CDK cheminformatics library within R

---
Rajarshi Guha [EMAIL PROTECTED]
GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
---
A beer delayed is a beer denied.

__
R-help@stat.math.ethz.ch 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.