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; i<n1; i++)
for(int j=0; j<n2; 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?