Accessing R objects in Java?

330 views
Skip to first unread message

Bill

unread,
Aug 25, 2010, 4:48:52 PM8/25/10
to Deducer
Hi, all,

I'm working on a way to produce more aesthetically pleasing output in
Deducer, such as by using HTML to display tables. The problem I'm
having is how to access an R object in Java. My major lead so far is
the toJava function in rJava (in R), which is designed to create an
REXPReference that can be accessed in Java. Let's say you run an
analysis of variance in Deducer and you give it the name aov1:

> aov1 <- k.sample.test(formula=d(pref) ~ size, data=candy, test=oneway.test)

Next you use toJava to pass that along to Java:

> jaov1 <- tojava(aov1)

The toJava documentation suggests using the following Java code:

public static REXP call(REXPReference fn) throws REngineException,
REXPMismatchException {
return fn.getEngine().eval(new REXPLanguage(new RList(new REXP[]
{ fn })), null, false);
}

I'm a newby in Java and I cannot for the life of me write the code
that would simply print the contents of the jaov1 object that I
created in R, such as by using a system.out.print.

Any suggestions?

Many thanks!
--Bill

Ian Fellows

unread,
Aug 26, 2010, 4:08:56 AM8/26/10
to ded...@googlegroups.com
Bill,

You can get an REngine java reference to the object (an REXP) with

REXP rexp = Deducer.eval("aov1");

For more information about working with R objects in Java check out
the java docs: http://www.rforge.net/org/docs/org/rosuda/REngine/package-summary.html

also look at the test.java class in rJava for examples.

the easiest way to get the output in java is

REXP rexp = Deducer.eval("capture.output(print(aov1))");

String[] s = rexp.asStrings();

or you can just print it to the console with:
Deducer.eval("print(aov1)");

toJava and REXPReference are generaly used for R functions. so,
alternatively you could get the print function as an REXPReference and
then pass rexp to it as a parameter.

note that I'm not by a real computer right now so I can't give working examples.


cheers,
ian

Bill

unread,
Aug 26, 2010, 1:22:11 PM8/26/10
to Deducer
Thanks, Ian. I used your suggestions to create a new class called
FromR, which I arbitrarily plopped into the models package. Here's
what I came up with:

package models;

import org.rosuda.REngine.*;
import org.rosuda.deducer.Deducer;

class FromR {
REXP rexp = Deducer.eval("capture.output(print(aov1))");
public void FromR() throws REXPMismatchException {
String[] s = rexp.asStrings();
System.out.print(s);

}
public static void main(String[] args) {
FromR fr = new FromR();
}
}

This compiles in NetBeans, but when I run it, I get the following
error:

Cannot find JRI native library!
Please make sure that the JRI native library is in a directory listed
in java.library.path.

java.lang.UnsatisfiedLinkError: C:\Documents and Settings\altermattw
\My Documents\NetBeansProjects\Deducer\JRI.dll: Can't find dependent
libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1728)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at org.rosuda.JRI.Rengine.<clinit>(Rengine.java:19)
at org.rosuda.deducer.Deducer.eval(Deducer.java:531)
at models.FromR.<init>(FromR.java:7)
at models.FromR.main(FromR.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

I scanned JRI.dll with Dependency Walker and found that it referenced
JVM.dll and R.dll (as well as a bunch of windows/system32 .dll files),
so I added the JVM and R .dll files to my NetBeans project directory,
but I'm still getting the same error. Any suggestions of other
dependent libraries, or perhaps modifications to the Java program that
would avoid this problem?

Thanks!
--Bill

Ian Fellows

unread,
Aug 26, 2010, 5:31:20 PM8/26/10
to ded...@googlegroups.com
Bill,

java classes are loaded from within R. You have to make an R package
with your code. See

http://www.deducer.org/pmwiki/index.php?n=Main.Development#suaptijc
"Compiling and JARing the code"

Also take a look at the DeducerPlugInExample package for a working example.

Ian

Reply all
Reply to author
Forward
0 new messages