Steps needed to Access and Collect data using APIs in R
To access and collect data using an API in R, you need to follow the following steps:
- Identify the API: Locate the API that offers the information you desire, then study its documentation to learn about its endpoints, parameters, and any authentication needs.
- Install the required software: Install the R packages needed to access and work with the API’s data. A few popular packages include httr, jsonlite, and XML.
- Set API parameters: Provide the necessary inputs for the API request, such as query parameters, endpoint URLs, and API keys or tokens. The HTTP request functions offered by the R packages normally receive these arguments.
- Make the API request: To submit the request and any relevant arguments to the API endpoint, use the appropriate HTTP request function from the R package.
- Parse and process the data: After receiving the API response, use R packages like jsonlite or XML to transform the data into manipulable R objects like data frames or lists.
- Data analysis and visualization: To carry out any required analyses or visualizations on the data gathered via the API, use R functions and packages.
Example:
In this example, we will be using the OpenWeatherMap API in R to fetch the current weather conditions for New Delhi.
Step 1: Install and load the necessary packages
First of all, we will install the necessary packages that are required to access and collect the data from APIs.
Syntax: install.packages("package-name") library(package-name)
R
# installing the package to fetch # the data from openWeatherMap API install.packages ( "httr" ) install.packages ( "jsonlite" ) # loading the packages library (httr) library (jsonlite) |
Step 2: Setting API parameters
The API key in the OpenWeatherMap can be found on the account page.
R
# setting the API paramater. # API endpoint URL url <- "http://api.openweathermap.org/data/2.5/weather" # Enter your api key api_key <- "Enter API KEY" # your preferred city city <- "New Delhi" # country code in which city is located country <- "IN" # creating a list of parmater to send with API request params <- list (q = paste (city,country,sep = "," ) , appid=api_key) |
Step 3: Making the API request
To make an API request, we will use the GET() method.
Syntax: GET(url, query = NULL, ...)
Parameters:
- url: It is the website link.
- query: It is a list of parameters that can be involved in the request.
R
# Making API request res <- GET (url,query=params) |
Step 4: Parse and Converting the data into an R object
To parse and convert the data into an R object, we will use fromJSON() method.
Syntax: fromJSON(txt, flatten = TRUE, ...)
Parameters:
- txt: It is the JSON-written document that will be converted into an R object.
- flatten: If a value is TRUE then it converts the nested list into a single list.
R
# Parsing and processing the data ans <- fromJSON ( content (res, "text" ),flatten= TRUE ) |
Step 5: Retrieving the data
Syntax: paste(string1,string2)
Parameters:
- string 1 and string 2 are two different string
R
# Converting the temperature into Celsius from Kelvin tem <- ans$main$temp - 273.15 # Printing the temperature print ( paste ( "The temperature is" , round (tem,2), "C in" , city)) |
Output:
"The temperature is 27.09 C in New Delhi"
Access & Collect Data with APIs in R
Application Programming Interface is referred to as API. Communicating and sharing data between various software systems is made possible by a collection of protocols, procedures, and building blocks for software applications.
You can access and gather data from a variety of sources, including social networking sites, news websites, online marketplaces, and many more, thanks to APIs. Sending an API endpoint a request and waiting for it to respond with the necessary data in a format like JSON or XML allows you to achieve this.