Building REST API using R Programming - GeeksforGeeks

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.
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.
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.
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 packageinstall.packages("plumber")  # Loading packagelibrary(plumber)
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 packagelibrary(plumber)  #* Revert back the input#* @param msg Input a message to revert#* @get /revertrevert <- function(msg = ""){  list(msg = paste0("The input message is: ",                                     msg, "'"))}  #* Plotting a bar graph#* @png#* @get /plotfunction(){  normal_func <- rnorm(60)  barplot(normal_func)}
server.R
R
# Loading packagelibrary(plumber)  # Routing APIr <- plumb("api.R")  # Running APIr$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.
R Data-science
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
How to change Row Names of DataFrame in R ?
Remove rows with NA in one column of R DataFrame
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
K-Means Clustering in R Programming
Logistic Regression in R Programming