Open In App

Building REST API using R Programming

Last Updated : 12 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

REST(Representational state transfer) API is an architectural style that includes specific constraints for building APIs to ensure that they are consistent, efficient, and scalable. REST API is a collection of syntax and constraints which are used in the development and operation of web services that include sending & receiving information through there endpoint i.e a URL providing an interface to the external environment.

REST was first presented by Roy Fielding in 2000. The abstraction of information in REST is termed as a resource. REST uses a resource identifier to identify the particular resource in an interaction between components. It allows an application to access resources or functionality available on another server which is remote to that application’s architectural and security domain.

Theory

REST uses resource methods to perform the desired transition. An API can only be considered RESTful when it meets the following conditions:

  • Uniform Interface: A well-defined interface between the server and the client.
  • Stateless: A state is managed via the requests themselves and not through the support of an external service.
  • Cacheable: Responses should be cacheable in order to improve scalability.
  • Client-Server: Well defined separation of client and server.
  • Layered System: Client should be unaware of intermediaries between the client and the server.
  • Code on Demand: Response can include logic executable by the client

The 4 most important HTTP protocol methods;

  1. GET: Retrieves data from a remote server and can be a single resource or a list of resources.
  2. POST: Creates a new resource on a remote server. *
  3. PUT: Updates the data on a remote server.
  4. DELETE: Deletes data from a remote server.

REST API can be used with any language as the requests are based on the universal HTTP protocol. In R Language, we use the httr package and jsonlite package to make a GET request, parse the results, and finally page through all the data. This requires converting the raw data from the GET request to JSON format and then into a parsed data frame.

Implementation in R

The Package

Plumber package is used to create REST API such as providing data for an analytics dashboard etc. It allows to easily convert R code into an endpoint.

R




# Installing the package
install.packages("plumber")
  
# Loading package
library(plumber)


Building API using package

Using the plumber package building an API. Making two files, one is my_api.r containing all endpoints and the second is server.r to load all endpoints of API by starting the server.

my_api.R

R




# Loading package
library(plumber)
  
#* Revert back the input
#* @param msg Input a message to revert
#* @get /revert
revert <- function(msg = "")
{
  list(msg = paste0("The input message is: "
                                    msg, "'"))
}
  
#* Plotting a bar graph
#* @png
#* @get /plot
function(){
  normal_func <- rnorm(60)
  barplot(normal_func)
}


server.R

R




# Loading package
library(plumber)
  
# Routing API
r <- plumb("api.R")
  
# Running API
r$run(port = 8000, swagger = TRUE)


Outputs:

  • Normal_func:

The normal function is output with rnorm().

  • Running API:

The api.R file is run with port 8000 and swagger is TRUE.

  • Revert() on Swagger:

The input message is asked to execute the function.

The input message is outputted in JSON format and response headers.

  • Plot() on Swagger:

The plot() is executed with Swagger UI and can also be executed using a Curl statement.

The Bar graph is outputted after running the plot().

The response headers are generated without a default response.

So REST API is used in the Software industry and IT industry with the full scope on a daily basis.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads