Yes, I know!Here’s the R file in question:Nifty!Place it in your resources folder, for easy access from the JVM side.
Now we can create a Main as usual and we’ll use Graal to execute the R file:The comments in the above Main.
scala file should explain what it does, but TL;DR: it uses GraalVM to run an R file, that prints “Hello, World!” and then returns a String, which is then printed by Scala.
Tadaa, we have just executed R code from a Scala project!.You can also do this from Java, or any other JVM language, or from JVM frameworks such as Spring Boot.
ConclusionEmbedding R code in a Scala/JVM project can be an excellent way to use the powerful data processing packages accessible in R (OCR processing, for instance) while using a language more suitable for integration in a services landscape to take care of integration with other moving parts.
It’s also possible to directly embed R code in Scala files, but the benefit of embedding complete R files rather than code snippets is that R code stays in its own files.
This means you (or your Data Scientist) can use a specialised editor (e.
RStudio) to create the R side of your functionality, while you can use your own favourite editor for the JVM side (e.
REMINDER: as stated at the start of this piece, GraalVM and its moving parts (like FastR) are still under development.
If you start using more advanced R packages, you may run into incompatibility issues, or bugs.
It is worth trying out, though!.Quite a few of them work!Github example project: https://github.
com/NRBPerdijk/example-graalvm-r-scalaClick here to see the other languages that GraalVM supports.
Click here to see what other use cases GraalVM has practical applications for.
.. More details