# Combining R and Java

Was curious if there were any libraries out there for combining R and Java so did some research to figure out the best library out there for this. Why? Since I know Java already combining it directly with R is something I was interested in. A use case could be if you have a mathematician who is great at using R to produce models but not so good at writing Java code to tie those models into your application. Combining R and Java(assuming your app is in Java) would would be an easy way to let the mathematician do his job and let the Java developers easily integrate the mathematician’s model.

Below are some of the libraries available for combining R and Java.

- Renjin : This is the library I went with. Library appears to be actively developed. They have a nice website and good docs teaching you how to use the library.
- RCaller: Was close to choosing this one. Library is actively developed and has good docs(didn’t try to use them but appear intuitive).
- JRI
- RServer
- rJava

Ended up using renjin as my Java to R library. This example is a simple web service that would allow a user RESTfully input ‘x’ and ‘y’ coordinates and generate a model using R. The webservice also provides functionality for making predicitons based on your model. I used Jetty, CXF, Renjin, Jackson and Maven in the example. The source can be found here. Here are some screen shots showing the example endpoints:

Renjin was pretty easy to use. But wish they had some more helper methods to get Vector or other custom data types they have into native Java Objects. Also don’t like that the toStrings() only list a certain number of variables(i.e. if you have a Vector and want to see all the ‘names’ it has). At some point I’ll sit down and figure out how to use Shiny but this could be a possible interm solution for using an R Script directly in your webapp.