Connecting to R from Java

Background: NetLogger uses R for analysis, but the Pegasus group has a Java GUI. They want to re-use the R code directly by connecting to R from Java.

I think this project (JRI) has what’s needed:

home: http://www.rforge.net/JRI/
download page: http://www.rforge.net/JRI/files/
javadoc page:http://www.rosuda.org/R/nightly/JavaDoc/

It compiled and seemed to work on Mac OSX (Jaguar, with Developer tools installed).

If we just want graphs from R, the basic interface is all we’d need. Here’s an example of how I ran a little test program (below) from inside the unzipped tarball. When it finishes you can look at the output in “/tmp/nlr.pdf”.


JAVA_HOME=/Library/Java/Home
R_HOME=/Library/Frameworks/R.framework/Resources
javac -classpath src/JRI.jar examples/nlr.java
java -cp src/JRI.jar:.:examples nlr
(Output)
creating Rengine
Rengine created, waiting for R
R is running
stopping Rengine

Test program:


import org.rosuda.JRI.Rengine;
import org.rosuda.JRI.RMainLoopCallbacks;

class EmptyCallbacks implements RMainLoopCallbacks {
static void print(String s) { System.err.println(s); }

public void rWriteConsole (Rengine re, String text, int oType) {
//print("Console: " + text);
return;
}
public void rBusy (Rengine re, int which) { print("busy"); return; }
public String rReadConsole (Rengine re, String prompt, int addToHistory) { print("read console"); return ""; }
public void rShowMessage (Rengine re, String message) { print("Message: " + message); return; }
public String rChooseFile (Rengine re, int newFile) { print("choose file"); return ""; }
public void rFlushConsole (Rengine re) { print("flush console"); return; }
public void rSaveHistory (Rengine re, String filename) { print("save hist"); return; }
public void rLoadHistory (Rengine re, String filename) { print("load hist"); return; }
}

public class nlr {
static void print(String s) { System.err.println(s); }
static void eval(Rengine r, String s) { r.eval(s, false); }

public static void main(String[] args) {
if (!Rengine.versionCheck()) {
System.err.println("** Version mismatch - Java files don't match library version.");
System.exit(1);
}

print("creating Rengine");

Rengine re = new Rengine(args, false, new EmptyCallbacks());

print("Rengine created, waiting for R");

// the engine creates R is a new thread, so we should wait until it's ready
if (!re.waitForR()) {
System.out.println("Cannot load R");
return;
}

print("R is running");

try {
eval(re, "data(iris)");
eval(re,"pdf('/tmp/nlr.pdf')");
eval(re, "print(stripchart(iris[, 1:4], method = 'stack', " +
"pch = 16, cex = 0.4, offset = 0.6))");
eval(re, "dev.off()");
} catch (Exception e) {
print("ERROR: "+e);
e.printStackTrace();
}

print("stopping Rengine");

re.end(); // stop the Rengine

return;
}
}

9 Comments

pingouMarch 23rd, 2009 at 10:46 am

I’m getting problem to set the R_HOME variable, how did you do ??

Thanks for the example though ;-)

dangMarch 23rd, 2009 at 10:54 am

If you are asking how did I find R_HOME, there is a built-in command, just run:
R RHOME

If you want to know how to set the environment variable: I’m assuming Unix, and in particular bash or sh. If you’re using csh or tcsh, replace
R_HOME=/Library/Frameworks/R.framework/Resources
with
setenv R_HOME /Library/Frameworks/R.framework/Resources

pingouMarch 31st, 2009 at 11:07 pm

I finally made it and see with the maintainer of R so that R_HOME is now defined while installing R itself.

But thanks for your answer :)

JaneApril 8th, 2009 at 1:28 am

Finally done. also thank you Dang for your great help above.

If you want to know how to set the environment variable: I’m assuming Unix, and in particular bash or sh. If you’re using csh or tcsh, replace
R_HOME=/Library/Frameworks/R.framework/Resources
with
setenv R_HOME /Library/Frameworks/R.framework/Resources

FreeWiiJune 22nd, 2009 at 9:20 am

Thanks the example was reall helpful! Finally managed it.

HookahJune 28th, 2009 at 8:51 am

Nice write-up, helps figuring out java

JuanSeptember 4th, 2009 at 2:48 pm

hello very interesting your example, where can I find more examples of JRI?
thanks for sharing your knowledge.

Keith SchillingOctober 18th, 2009 at 3:06 pm

hhmm..I’m not getting the same. Would snow leapord have something to do with the functioning of the script?

JonNovember 16th, 2009 at 3:52 pm

Ok, so it took me some time to figure this out…. >=[ I was doing it on a Mac (not the best mac user), but I was able to get it working. Gunna grab a beer after all that lol

Leave a comment

Your comment

  WordPress version 2.8.4