Reputation: 4663
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:
Run commands interactively not only ready-made R scripts.
Produce and handle graphics from the established graphics packages.
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
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.
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
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