Java-R Integration

Java-R integration?

I have successfully used two alternatives in the past.

JRI

  • Pros: probably better performance.
  • Cons: you have to configure some environment variables and libraries, different in Win/UNIX.

RServe

  • Pros: easy to setup, you don't need to initialize R or link against
    any R library, can run in a different machine.
  • Cons: based on TCP/IP (a server is running), no callbacks from R.

Other alternatives I have never used : RCaller

R integration with Java

  1. rServe is a client-server communication, the R code runs on server and client(here java) gets the results, using usual TCP/IP connection (can also run locally).
  2. rJava is more like a API library to R in java. The R code runs on your locally installed R instance.

selecting one of them is based on your needs. If you want to embed a small amount of R code in java then rJava is the correct choice. If you have large amount of R codes in multiple scripts, or if you want them to be exposed as REST API to multiple java clients then rServe is the way to go.

Integrate R in java web application

To locally call R from Java, you need JRI. To remotely call Java you can use RServe. If you want to handle graphics, that is best done using something in R such as the evaluate package. Afaik most language bridges offer no particular functionality for handling graphics, you will need to do that on the R level.

Have a look at this paper before you get started to be aware of the challenges and limitations of using cross language bridges for scientific computing. You'll save yourself a lot of trouble down the line.

Java and R integration

Thanks for updating your question with some example code. I cannot completely replicate your circumstances because I presently don't have immediate access to Amazon EC2, and I don't know the specific type of instance you are using. But here a couple of suggestions for de-bugging your issue, which I have a hunch is being caused by a missing package.

1. Try to install the offending packages via your R script

At the very beginning of your R script, before you try to load any packages, insert the following:

install.packages(c("XLConnect", "rJava"))

If your instance includes a specified CRAN mirror (essentially, the online repository where R will first look to download the package source code from), this should install the packages in the same repo where your other packages are kept on your server. Then, either library or require should load your packages.

(sidenote: rJava is actually a dependency of XLConnect, so it will automatically load anyway if you only specify library(XLConnect))

2. If the above does not work, try installing the packages via the command line

This is essentially what @Ben was suggesting with his comment. Alternatively, see perhaps this link, which deals with a similar problem with a different package. If you can, in terminal on the server, I would try entering the following three commands:

sudo add-apt-repository ppa:marutter/rrutter
sudo apt-get update
sudo apt-get install r-cran-XLConnect

In my experience this has been a good go-to repo when I can't seem to find a package I need to install. But you may or may not have permission to install packages on your server instance.

regarding integrating R into Web-applications

Have you looked at RSS, Rweb, R Node, etc?

http://www.unt.edu/rss/Rinterface.htm

http://www.math.montana.edu/Rweb/

http://cran.r-project.org/doc/FAQ/R-FAQ.html#R-Web-Interfaces

http://www.decisionstats.com/r-node-and-other-web-interfaces-to-r/

http://sysbio.mrc-bsu.cam.ac.uk/Rwui/

How can I integrate Java with R on a remote machine?

There is Rserve software that allows to use computer with R installed as remote computation server. There is also REngine Java client bundled with RServe that allows seamless integration of Rserve server and your Java code. Client has robust API and performs automatic type conversion between R and Java types.

After you install Rserve and put it's client on classpath of your Java code you can perform remote R computations as easy as

RConnection c = new RConnection("<your Rserve host>", <your Rserve port>);
double d[] = c.eval("rnorm(10)").asDoubles();

Passing an R function as a Java method parameter

Assuming you're using the REngine API you want to construct a call to the function an evaluate it:

// get a reference to the function
REXP fn = eng.parseAndEval("function(x) x ^ 3 - x ^ 2 - 4 * x + 2", null, false);

// create a call and evaluate it
REXP res = eng.eval(new REXPLanguage(new RList( new REXP[] {
fn, new REXPInteger(5)
})), null, true);
System.out.println("Result: " + res.asDouble());

and you'll get

Result: 82.0

Obviously, you can use new REXPSymbol("f") instead of fn if you want to keep the function as f on the R side.

PS: if you want quick answers, consider using the rJava/JRI mailing list stats-rosuda-devel.



Related Topics



Leave a reply



Submit