How to build a REST API using R
Plumber is an R package that allows you to create a REST API using R. You can find a basic example/ boilerplate project here https://github.com/tunnelWithAC/r-boilerplate-api.
I first came across Plumber last year when I worked on a project that involved building a REST API using R to provide data for an analytics dashboard. I had limited experience with R before this project and it was my first time working with the Plumber package
R is most commonly used for statistical analysis and R Shiny is the most common method of building web apps using R. There is limited documentation and examples of R being used for building APIs using Plumber or other packages.
Based on my experience, these are what I consider to be the main pros and cons of using R/ Plumber to build REST APIs
Pros:
- Easily convert existing R code to API endpoints
- Cheaper alternative to building Shiny dashboards
Cons:
- Limited documentation and community support
- Difficult to manage package versioning
The main benefit of R is that allows you to easily convert existing R code to API endpoints
Previously it was very difficult to make results from R analysis available via API. One solution was to rewrite the analysis in another language, such as Python. Having to rewrite code in another language is inefficient, Plumber allows you easily convert R code to an API endpoint as shown below.
Example API
Make sure you have plumber installed by running the code below in your R Studio console
install.packages("plumber")
We need two files for this example. One file containing our endpoints and another that will load these endpoints and start a server.
# api.R
#* @get /add
add <- function(x, y){
return(as.numeric(x) + as.numeric(y))
}#* @get /add2
add2 <- function(x, y){
list(result = as.numeric(x) + as.numeric(y))
}
As you can see in the example above. If the return keyword is not used the value from the last line in the function is returned.
The first add function returns the value by itself in an array. The list function is used in the second add function to return the result in JSON format.
Example responses
$ curl "http://localhost:8000/add?x=10000&y=50"
[10050]$ curl "http://localhost:8000/add2?x=10000&y=50"
{"result": [10050]}
You may have noticed that API responses generated from Plumber render singular values as arrays. You can add the code below above your endpoint if you don’t want values to be returned as arrays. More information on why values are returned as arrays can be found here.
#* @serializer unboxedJSON
Run the code below in R Studio to start the server or run Rscript serve.R
# serve.R
library(plumber)
r <- plumb("api.R")
r$run(port=8000)
Swagger
Swagger UI allows anyone to visualize and interact with the API’s resources without having any of the implementation logic in place.
How to use the Swagger UI
All you need to do to enable the Swagger UI is to pass the swagger=TRUE parameter to the run function in your serve.R file as shown below
r$run(port=8000, swagger=TRUE)
When you start your server the UI should pop up automatically, if it doesn’t you can open it in your browser at this URL
http://127.0.0.1:8000/swagger.json?schemes=http&host=127.0.0.1:8000&path=/
Part two of this article covers Authentication, Docker, creating a live reloading server and more.
Thanks for reading! If you’ve any questions or suggestions leave a comment below.