Building REST API using R Programming
Last Updated :
12 Oct, 2020
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;
- GET: Retrieves data from a remote server and can be a single resource or a list of resources.
- POST: Creates a new resource on a remote server. *
- PUT: Updates the data on a remote server.
- 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
install.packages ( "plumber" )
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
library (plumber)
revert <- function (msg = "" )
{
list (msg = paste0 ( "The input message is: " ,
msg, "'" ))
}
function (){
normal_func <- rnorm (60)
barplot (normal_func)
}
|
server.R
R
library (plumber)
r <- plumb ( "api.R" )
r$ run (port = 8000, swagger = TRUE )
|
Outputs:
The normal function is output with rnorm().
The api.R file is run with port 8000 and swagger is TRUE.
The input message is asked to execute the function.
The input message is outputted in JSON format and response headers.
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.
Similar Reads
Accessing REST API using R Programming
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 tha
3 min read
Reading Files in R Programming
So far the operations using the R program are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. So the two most common o
9 min read
Writing to Files in R Programming
R programming Language is one of the very powerful languages specially used for data analytics in various fields. Analysis of data means reading and writing data from various files like excel, CSV, text files, etc. Today we will be dealing with various ways of writing data to different types of file
2 min read
Basic Syntax in R Programming
R is the most popular language used for Statistical Computing and Data Analysis with the support of over 10, 000+ free packages in CRAN repository. Like any other programming language, R has a specific syntax which is important to understand if you want to make use of its powerful features. This art
3 min read
Working with CSV files in R Programming
In this article, we will discuss how to work with CSV files in R Programming Language. R CSV Files R CSV Files are text files wherein the values of each row are separated by a delimiter, as in a comma or a tab. In this article, we will use the following sample CSV file. Getting and Setting the Worki
4 min read
8 Coding Style Tips for R Programming
R is an open-source programming language that is widely used as a statistical software and data analysis tool. R generally comes with the Command-line interface. R is available across widely used platforms like Windows, Linux, and macOS. Also, the R programming language is the latest cutting-edge to
5 min read
Working with Databases in R Programming
Prerequisite: Database Connectivity with R Programming In R programming Language, a number of datasets are passed to the functions to visualize them using statistical computing. So, rather than creating datasets again and again in the console, we can pass those normalized datasets from relational da
4 min read
Working with XML Files in R Programming
XML which stands for Extensible Markup Language is made up of markup tags, wherein each tag illustrates the information carried by the particular attribute in the XML file. We can work with the XML files using the XML package provided by R. The package has to be explicitly installed using the follow
3 min read
Working with JSON Files in R Programming
JSON stands for JavaScript Object Notation. These files contain the data in human readable format, i.e. as text. Like any other file, one can read as well as write into the JSON files. In order to work with JSON files in R, one needs to install the "rjson" package. The most common tasks done using J
4 min read
R6 Classes in R Programming
In Object-Oriented Programming (OOP) of R Language, encapsulation means binding the data and methods inside a class. The R6 package is an encapsulated OOP system that helps us use encapsulation in R. R6 package provides R6 class which is similar to the reference class in R but is independent of the
3 min read