Enhance your Java Spring application with R data science

Oleg Šelajev
May 10, 2018 · 5 min read

In this post we look at GraalVM running a sample Spring application written in Java, that is using ggplot2, an R library, to plot the data.

GraalVM is a polyglot virtual machine capable of running various languages efficiently such as Java, Scala, JavaScript, Ruby, R and native languages compiled to llvm bitcode..

What is even more interesting is that GraalVM allows you to mix languages in a single program. There’s no performance hit for having Java or JavaScript, or Ruby, or R call each other or operate on the same data together. Let’s examine how to implement this interoperability between Java and R in a Spring boot application.

Why can’t we stick to just 1 language?

For example, say we want an application with a simple visual output. Plotting data is very easy in R, so we chose R. ggplot2 is a very popular R package for plotting all kinds of data, which sounds exactly like the right tool for the job!

Now, we don’t expect that every application out there would require a data plotting ability, or should become a polyglot application immediately. But we strongly believe that every programming language has something unique to offer. And it is more efficient to re-use existing solutions than recreate them from scratch.

Java + R Spring application

In order to run it, you will need a runtime capable of running both Java code (JVM bytecode) and R.

Download either the community or enterprise edition of GraalVM (both will work). We’re using the enterprise edition here on macOS.

Unpack the downloaded binary and set GRAALVM_HOME to point to the directory in which it was unpacked.


Also export JAVA_HOME=$GRAALVM_HOME to be make sure Maven will find the graal-sdk.jar file with the GraalVM polyglot API classes.

export GRAALVM_HOME=/path/to/GraalVM

Clone the demo app repository and navigate to the spring-r directory:

git clone https://github.com/graalvm/graalvm-demos
cd spring-r

Since this Java application uses the ggplot2 R library, you need to install R language support:

gu -c install org.graalvm.R

Install R dependencies for the demo app. As mentioned, it uses ggplot2, a very popular package for visualizing data. The following command will download the sources for the ggplot2dependencies, and place them under the GraalVM distribution. (Note: it can take a couple of minutes.)

Rscript -e "install.packages(\"ggplot2\")"

Now we can finally run the demo application:

mvn spring-boot:run

Wait a couple of seconds for Spring to initialize and then open http://localhost:8080/load in your browser.

The page will load an svg image and the HTTP headers tell the browser to refresh the page every second.

After a couple of seconds, you should see a picture like this:

Java — R interop in GraalVM

So far, so good. Now how can we use this function from Java? Since we’re dealing with a Spring app, we can define the components necessary for the GraalVM’s interop as Spring beans.

To use the GraalVM polyglot functionality, we need to define a context for the dynamic languages used from Java (it actually works in a similar way for all language interop in GraalVM).

Next, we use this context to evaluate our R file. Since it only declares a single function, it is the result of the ctx.eval() call, so we can convert the polyglot value to an instance of the java.util.Function class.

Note, that to enable our app the access to the R file, we place it into the resources directory which will be on the class path. Then we configure access to it by annotating the Resource field with @Value.

Note, if you’re going to modify this code, Spring has a class called Value, the annotation used above, and GraalVM polyglot API also has a Value class for representing values in the polyglot context. Be careful not to mix or confuse these.

Now comes the best part, the R function defined in a separate R file is exposed as a normal Spring bean, so we can declare a field of the correct type and wire it to be provided for us by the Spring dependency injection mechanism.

private Function<Double, String> plotFunction;

When the app is being initialized, the plotFunction field will be set to the result of evaluating our R source file. The rest of the app is straighforward. We define a request mapping in a controller that serves the svg file generated by our plotFunction function to the browser:

There’s a couple of very interesting things happening in this code. During execution we cross the language barrier from Java to R and back to Java. Note that the plotFunction function takes a normal Java double as the argument, and it returns a normal Java String. The R code operates on these values automatically, without a need for conversion.

For example, we pass a DataHolder object to R:

public static class DataHolder {
  public double value;
  public DataHolder(double v) {
    value = v;

In the R code we access the DataHolder.value field using the normal R field access pattern:

function(dataHolder) {
  data <<- c(data[2:100], dataHolder$value)

The interop works the other way too. For example it makes sense to log all the things through the Java logger for consistency.

For example, we have the following LogHolder class:

The following short snippet allows R code to access the Java LogHolder type and call its static log method:

Impressive, isn’t it?


We’re very curious to learn what you would like to add to your Java applications from other languages’ ecosystems and try make it possible with GraalVM. If you have ideas or want to ask something please reach out! For example @shelajev would always love to chat, ping him.

If you want to check out what else can GraalVM do, read the “10 things GraalVM can do” blog post by Chris Seaton.


GraalVM: This Spring bean in an R function.


GraalVM team blog - https://www.graalvm.org

Oleg Šelajev

Written by

Developer advocate for GraalVM at OracleLabs. VirtualJUG organizer. Became Java Champion in 2017. Likes programming languages, compilers, performance, chess



GraalVM team blog - https://www.graalvm.org