Creating a R web application with Nuxt and Plumber

Published Nov 08, 2017Last updated Mar 19, 2018

The Challenge

R is an open source programming language and software environment for statistical computing and graphics. Today R is often developed by scientists, statisticians or analytics on a desktop using scripts. It is not made for web application in the first place. However, there's increasingly a need for the interaction between both. Shiny is a popular option for delivering R in web apps currently. Though, Shiny is an opinionated framework as it has it own way generating HTML and CSS for you. It seems impossible to create your own frontend interface.

Solutions

There are couple of options if you are looking for a micro framework instead of a fullstack framework like Shiny. You can develop the stacks of technology by your own and that give you more flexibility. They are Plumber and jug. The main problem with them is the lack of community support and their documentations seems incomplete and under development. Below are some example codes how they work.

The frontend

Now, you have the basic idea how you can use either of these as your backend stack. As for the frontend, you can choose Nuxt, Next, Angular Universal, or just a plain HTML + CSS. I have been exploring Nuxt, so this article shows you how you can integrate it with R for a web application. I chose Plumber for this exercise.

The concept

The idea of this solution is using Nuxt to run as the view for the backend, R. We only use Nuxt routes for public users. axios is used inside Nuxt pages to call the backend routes. Then the backend spits JSON or image data to the pages in Nuxt, an example:

Access it at http://localhost:3000/. You should see a home page with Hello World message coming from the separate port at http://localhost:8000/:

If you access the app at http://localhost:3000/iris, you should see the iris dataset has been plotted into a graphic which is coming from http://localhost:8000/iris:

Conclusion

None of these frameworks is perfect. I have been looking for a solid R micro framework that is similar to Slim, Express or Koa, but it seems that Plumber or jug is the closest option if you have struggled with Shiny and I hope this article helps. Let me know what you think and what technologies you would use for your projects. Any suggestions and corrections, please leave a comment below.