Creating a Web Service in R

R is a powerful language and environment for statistical computing. It excels at crunching data and performing complex modeling. In application development, its strength is in creating the ”back end” components.

web services in R

Shiny Server allows R developers to create web applications using R. It provides R functions to create pretty sophisticated user interfaces and link them to the rest of the R ecosystem. This makes Shiny excellent for developing web-based analysis tools or interactive dashboards.

However, it would be ridiculous to assume that Shiny Server could match the range of components and libraries produced by the JavaScript community. This may be the largest ecosystem available to any development platform.

Shiny allows the development of hybrid JavaScript and R applications, but things start to get more complicated at this point—and there are few developers with extensive experience in both JavaScript and R. Also, Shiny Server tends to play better with jQuery. Linking with modern frameworks like React and Angular is more complicated—although support for this seems to be under consideration.

A simpler architectural model would be to have each environment do what it does best—React or Angular on the front-end, handling the UX, and a web service in R exposing data and analysis services.

So, what options are there for creating web services in R?


One option is OpenCPU. From their website…

OpenCPU is a system for embedded scientific computing and reproducible research. The OpenCPU server provides a reliable and interoperable HTTP API for data analysis based on R.

It’s designed to allow seamless integration between JavaScript and R. Web services must be written as R packages, which adds some extra work for developers, but this is a best practice anyway—so a sensible way to produce production web services.


DeployR is a commercial (Microsoft) integration technology for deploying R analytics. You can use it to access R services from within web, mobile, and desktop applications. If you are looking for a corporate solution, and are already using Microsoft server products, this might be a sensible direction.


OpenCPU and DeployR require servers to be configured. Is there an easier solution? Yes, there is—Plumber.

Plumber decorates ordinary R functions with special comments to expose them as REST web service calls. Here’s an example:

#* @get /normalDistribution
normalDistribution <- function(n = 10) {

The last three lines are just a standard R function. To add this to the web service API, we precede it with a comment that denotes whether it’s a GET or POST endpoint and provides the name of the resource.

To start the web service we save the web API resource functions in a file (e.g. api.R) and execute the following commands in the R console:

api <- plumb("api.R")

Then, if I browse to http://localhost:8080/normalDistribution I get the following JSON array returned


If I only want five values, I can pass n=5 as a query parameter


As simple as it could possibly be.

R is single-threaded, so can only work on one API request at a time. There are ways of addressing this, but, if you need high performance, it might be worth looking at the other solutions mentioned.

Recommended Learning Tree courses

If you are interested in learning more about R you may wish to consider the following Learning Tree courses

Learn How to Answer Key Organizational Questions with R Data Analysis

View Course

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.