You are here

Integrating R with a web application

Raising R from standalone to Web application, finally one tutorial for doing it.

In the previous tutorial, we saw how to install R on a server and how to generate good quality graphs. Today, we'll go one step further by learning how to call our R server from a Java web application, which is said to be technically challenging.
Up to day, there were several tries and methods to provide easy access to R from web applications, and they are listed in the R FAQ. For those seeking a framework, you might like to see Shiny from RStudio. However, if you enjoy putting the hands on and building your own Java code, this tutorial will teach how to reach this goal with simple and reusable technology.
At the end of this tutorial, we will have the same result as in the previous one, a nice pie-chart, served by a production-ready web application hosted on Tomcat.

  1. Extending R with rJava

    The first step is extending R with the rJava package. It's so easy :

    # wget
    # R CMD INSTALL rJava_0.9-7.tar.gz
    # R CMD javareconf
  2. R accessibility

    In order to make our Tomcat use R, we need to build the R accessibility. We do this this by adding the library package (jar file), and more uncommon to Tomcat developers, setting system environment variables. In the Tomcat file, add this :

    export LD_LIBRARY_PATH
    export R_HOME

    In the Tomcat lib directory, add this file :

  3. Java code

    Before starting to code anyhting, we must know that the R doesn't support multi-threading as explained in the Java Rengine class documentation. As we are going to make individual calls to R, we must implement a singleton to avoid threading problems.

    package com.river_tiger.rdemo;
    import org.rosuda.JRI.Rengine;
    public class rInterfaceHL {
      private static rInterfaceHL theInstance;
      private final Rengine engine;
      private rInterfaceHL(Rengine engine) {
        this.engine = engine;
        this.engine.idleDelay = 500;
        Rengine.DEBUG = 10;
      public static synchronized rInterfaceHL getInstance() {
        if (theInstance == null)
        return theInstance;
      public static synchronized void initialize(String loopback) {
        if (theInstance != null)
          throw new IllegalStateException("already initialized");
        theInstance = new rInterfaceHL( new Rengine( new String[] {"--vanilla"}, false, null ) );
      /* wrapper methods for Rengine */
      public synchronized long getVersion() {
        return Rengine.getVersion();
      public synchronized void eval(String s) {
      public static synchronized Rengine getMainEngine() {
        return rInterfaceHL.getMainEngine();
      public static synchronized boolean waitForR() {
        return rInterfaceHL.waitForR();
      public synchronized void destroy() {

    We also need some classes to package the graphs building, and it will make things easier later. In order to have light and readable code, we implement a Facade pattern.
    Of course, the graphs will be stored as files, and we must use a parameter ('fileout')
    package com.river_tiger.rdemo;
    public class rGraphScript {
      private String   rGraphTitle  = null;
      private String[] rGraphScript = null;
      public rGraphScript(String title, String[] script) {
        this.rGraphTitle  = title;
        this.rGraphScript = script;
      public String getrGraphTitle() {
        return rGraphTitle;
      public String[] getrGraphScript() {
        return rGraphScript;
    package com.river_tiger.rdemo;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Vector;
     * this class packages the graphs making
    public class rGraphScriptProducer {
       * produces a pie chart of the Top 5 Champions League clubs
      public static rGraphScript getTop5PieChart(String fileout, int[] p) {
        Vector v = new Vector();
        v.add( "library(Cairo)" );
        v.add( "CairoFonts(regular='Verdana:style=regular', bold='Verdana:style=bold')" );
        v.add( "Cairo('" + fileout + "', type='jpeg', quality=100, width=500, height=500)" );
        v.add( "slices < c(10, 7, 5, 5, 5)" );
        v.add( "lbls < c('Real Madrid 10', 'Milano 7', 'Bayern Munich 5', 'Barcelona 5', 'Liverpool 5')" );
        v.add( "pie(slices, labels = lbls, main='Champions League top 5', clockwise=TRUE)" );
        v.add( "" );
        return new rGraphScript("Champions League top 5", (String[])v.toArray(new String[0]));
  4. Front end code

    Of course, we need a Servlet for handling client requests, and we associate it to a JSP for the rendering. This is pretty easy, just remember that we need to know where graphs are stored.
    package com.river_tiger.rdemo;
    import java.util.HashMap;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import com.river_tiger.rdemo.rGraphScript;
    import com.river_tiger.rdemo.rGraphScriptProducer;
    import com.river_tiger.rdemo.rInterfaceHL;
    public class RServlet extends HttpServlet  {
      private static rInterfaceHL re  = null;
      private static String R_OUT_FSHOME = null;
      private static String R_OUT_RDIR   = null;
       * initializes the R singleton,
       * reads the context parameters
      public void init(ServletConfig config) throws ServletException {
        re = rInterfaceHL.getInstance();
        R_OUT_FSHOME = config.getInitParameter("R_OUT_FSHOME");
        R_OUT_RDIR   = config.getInitParameter("R_OUT_RDIR");
        System.out.println("R_OUT_FSHOME:" + R_OUT_FSHOME);
        System.out.println("R_OUT_RDIR:" + R_OUT_RDIR);
      public void destroy() {
        if(re != null)
      public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        int[] top5Champions = new int[]{};
         * generate R script
        String fileName         = "example.jpg";
        String rFileOut         = R_OUT_FSHOME + R_OUT_RDIR + "/" + fileName;
        rGraphScript rScript    = rGraphScriptProducer.getTop5PieChart(rFileOut, top5Champions);
        req.setAttribute("R_outfile", R_OUT_RDIR + "/" + fileName);
        req.setAttribute("R_title", rScript.getrGraphTitle());
         * generate R image
        for(String rLine : rScript.getrGraphScript())
        // send to JSP
        req.getRequestDispatcher("/render.jsp").forward(req, res);
      public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        this.doGet(req, res);
        <img src="<%=(String)request.getAttribute("R_outfile")%>" alt="<%=(String)request.getAttribute("R_title")%>">
  5. Deployment descriptor

    As for any Java web application, we need a deployment descriptor (web.xml). This is the glue between the application server (Tomcat) and the Java code. Here it is :

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns=""
      <display-name>rdemo Application</display-name>
        A scriptable management web application for the Tomcat Web Server;
        Manager lets you view, load/unload/etc particular web applications.
      <!-- Define the Manager Servlet Mapping -->

Once all this is done, you will get your graphs produced by the web application and stored in the R_produced directory of your webapp.
You can download all the source code from the attachment and use it for any purpose.


Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer