Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend my Java application with R?

I am building an application that I want to have extended with modules that does some nr crunching and I would like to have R for that. What are my best options for extending my Java application with R?

like image 263
Per Arneng Avatar asked Jul 16 '09 10:07

Per Arneng


2 Answers

Talking to R from Java has been standardised using the REngine API. There are basically two implementations for this.

The first implementation is JRI. It is based on JNI, and executes the R dll inside the JVM. This means the connection is very fast. You can use full R functionality, including objects which live inside R but are accessible/modifiable in Java. The disadvantage is you cannot use multithreading.

The second implementation is RServe. RServe consists of a server written in C, together with a Java client. The server is started from the command line, and includes the R dll. The java client then makes a socket connection and calls R in a serialised manner. This implementation works well. The disadvantage is that, on Windows, the RServe component is not able to fork itself to handle multiple connections. Every RServe instance can only server one single user.

An alternative implementation to look out for is a Java RMI client which calls a Java server calling R using JRI. The idea is that you can use multithreading because you can talk to multiple servers at once. See http://www.londonr.org/remoterengine-londonR.pdf

In practice, we have used RServe together with a lot of boilerplate code for launching and managing the RServe instances. It's basically a major PITA, but it works well and has good performance.

like image 169
parasietje Avatar answered Oct 08 '22 14:10

parasietje


I've had a good experience integrating JGR, the Java Gui for R into my java application.

Note that the REngine is not multithread-safe. Thus, you need serialize access to the REngine (by, for example, letting it run in it's own thread). Your application and JGR would both update R variables and data frames using JRI. Now the great part about using JGR is that an R console is available so that the user can access the data being updated via your app, play with it, even change it on the fly, plot it, etc;! This combination of compiled (Java) and interpreted (R) modes is quite satisfying in terms of user experience.

Also, it looks like the JGR project is quite alive; I was using JGR 1.4, now it's at 1.7 (updated June 2009), so by all means, download it and give it a try.

If that works well and you're getting ambitious, look at biocep.

like image 35
vijucat Avatar answered Oct 08 '22 13:10

vijucat