kliron
kliron

Reputation: 4663

Integrate R in java web application

I know there are multiple questions resembling this one but most of them are >2 years old and actually not that related.

What I need to do is fully integrate the R environment in an existing java (actually scala) application. I don't want any R-based web solutions like Rook, Rapache and the like, the server logic happens strictly in java land. What I need is a way to send R commands to the interpreter, let it run them and handle the output. More importantly, I need to be able to:

  1. Run commands interactively not only ready-made R scripts.

  2. Produce and handle graphics from the established graphics packages.

  3. Communicate raw data back and forth between the JVM and R interpreter.

    I am aware of JRI. I would very much appreciate to hear from anyone who has used it. How stable is it? How actively maintained is the project? Any existing code that I can look at? Any other alternatives out there?

Upvotes: 1

Views: 2161

Answers (2)

Steves
Steves

Reputation: 3224

You can also consider FastR, a GraalVM based implementation of R. This is a screenshot from Swing application showing a lattice plot from R.

enter image description here

FastR can be embedded like so:

Context ctx = Context.newBuilder("R").allowAllAccess(true).build();
ctx.eval("R", "sum").execute(new int[] {1,2,3});

You can instruct the R script to plot into given Graphics2D object:

ctx.eval("R", "function(g) awt(g, 400, 400)").execute(myGraphics2DObject)
ctx.eval("R", "grid.points(1:10)")

The full example is on GitHub: https://github.com/graalvm/examples/tree/master/fastr_javaui

More info about FastR: https://medium.com/graalvm/faster-r-with-fastr-4b8db0e0dceb

Upvotes: 1

Jeroen Ooms
Jeroen Ooms

Reputation: 32978

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.

Upvotes: 3

Related Questions