1

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?

2
  • Even though your question goes beyond the "how to integrate R in Java", it helps to link some related questions [1] [2] [3].
    – bluenote10
    Oct 9, 2014 at 15:01
  • Well, it is actually 3 questions. I would be interested to hear if/how anyone can deal with point 2 above.
    – kliron
    Oct 9, 2014 at 15:43

2 Answers 2

3

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.

1
  • I really appreciate your answer. I won't mark the question as answered yet to see if some similarly useful and up-to-date answers can be collected here for reference. Thank you very much.
    – kliron
    Oct 9, 2014 at 16:17
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.