[Rd] Call R function from Java

Simon Urbanek simon.urbanek at r-project.org
Fri Jul 14 02:27:29 CEST 2006


Adam,

[I'm moving this discussion to stat-rosuda-devel, because JRI is  
strictly speaking a contributed package]

On Jul 13, 2006, at 5:28 PM, Adam Kapelner wrote:

> It worked. All I had to do is set PATH = < R directory > in my  
> system variables in "My Computer" and that was it!
>
> Oh btw Simon, I couldn't find the run.bat in the JRI_0.2-4.tar.gz  
> file. I found a "run.in" but that is a unix bash script.
>

run (and run.bat respectively) are created by make, so you should run  
"sh configure.win; make" on Windows or "./configure; make" on unix.  
The script must contain paths to your JRE and R, so it cannot be  
shipped directly in JRI - that's why it is created during the build  
phase instead.


> Another question:
>
> When I'm evaluating R expressions:
> 	REXP
> rexp3=r.eval("cancer=read.table(\"CancerCellsTrainingFile.txt 
> \",header=TRUE,
> sep=\",\")");
> 	System.out.println(rexp3);
>
> I sometimes get back form the println:
> 	RXP[unknown/19, id=1208787616, o=null]
>
> Is there anyway to actually view that output as a string?

It is a generic vector (aka list). If you use JRI 0.3 (from http:// 
www.rosuda.org/R/nightly ), you can use
RVector v=rexp3.asVector()
for (Enumeration e = v.elements() ; e.hasMoreElements() ;)
System.out.println(e.nextElement());

If you are using JRI 0.2 or earlier, you'll have to use something  
like this:
long[] l = r.rniGetVector(rexp3.xp);
int i=0; while(i<l.length) System.out.println(new REXP(r, l[i++]));

Cheers,
Simon



More information about the R-devel mailing list