library(tidyverse) # All the tidy things
library(jsonlite) # Converting json data into data frames
library(magrittr) # Extracting items from list objects using piping grammar
library(httr) # Interacting with HTTP verbs
weather_endpoint <- "http://api.weatherapi.com/v1/forecast.json?"
## define weather key in vector here
location <- paste("&q=London")
weather_api_url <- paste(weather_endpoint,weather_key,location,sep = "")
weather_api_urlAssignment 6
Introduction Weather API
This API provides weather data from many locations across the globe. You may choose your location as part of the API and also many other things. Some of the main information is the forecast up to 14 days in that specific location. You may also get other information such as air quality or when is sunrise. So, if you want to know anything around the weather for a certain location this is a great API for that.
Setting up API URL
To access the API you need a key which you can get by signing up on the website for free. I have already done this and the key I use is my personal key. I have hidden this key though for privacy. Once we get this key we need to make a command to create a url using this key. Before the API key is the endpoint which is given to you on the website. After v1 in the endpoint you can specify forecast or alerts and more. I have chosen forecast for this example. After the question mark is where you put key= and your own api key. We can now start to add parameters. We need the &q for this as you can see, which is also specific to this API. We can add more parameters, but for now lets just start with location. The weather_api_url will now give you a url that shows the forecast today in London.
Putting API URL in R
We now need to get the information from the url into a nice and tidy data frame in R. We add the newer_data frame vector as this should show up in our global environment in r as a data frame. We use the weather_api_url vector from earlier to make the URL. We then pull the data specifically from forecast and forecast day to get the weather for that day.
newer_data <-
weather_api_url %>%
GET() %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON() %>%
use_series(forecast)%>%
use_series(forecastday)Turn API into a Function
We can now combine the last two steps all into one function to condense this code. As you can see on the second line this function will have three inputs being API key, location, and days. The days is the number of days you want your forecast to be up to. The max is 14 days. We could add more inputs to this function but we will try to keep this simple for now. As you see at the very bottom you can test the function to see if you get a data frame in R. You should get 1 observation which is the forecast for today since the days vector = 1.
newer_function <-
function(weather_key,location,days){
weather_endpoint <- "http://api.weatherapi.com/v1/forecast.json?"
location <- paste("&q=",location,sep = "")
days <- paste("&days=",days,sep = "")
weather_api_url <- paste(weather_endpoint,weather_key,location,days,sep = "")
weather_api_url
newer_data <-
weather_api_url %>%
GET() %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON() %>%
use_series(forecast)%>%
use_series(forecastday)
return(newer_data)}
days <- 1
test_function <-
newer_function(weather_key,
"London",
days)Create a loop in R
We now can create a loop so that the function pulls all of the data from the API. I filtered the cities in the loop to for the weather. I also created a new data frame called new data which the data will go in. I then use a for loop to go through each city to get information from. As you can see I used the function created previously in this loop. The mutate row is to create a new column in the data frame denoting which city is described.
New_data <- data.frame()
for (location in c("London", "Cincinnati")) {
New_data <- newer_function(weather_key,
location,
14) %>%
mutate(city = location)%>%
## add a new column to denote which city weather is from
bind_rows(New_data) }Example of Calling API
An example of this API is if we wanted to get the forecast of the weather in London and Cincinnati for the next 14 days. For the sake of this example lets say I just want this information and not air quality information which you can add if you like. If you wanted this you would just need to put &api=yes in the end of your url. We combine the function we created earlier with the loop we did in the last step. I changed the days in the input of the function to 14 and defined it as a vector since we want the forecast for 14 days. Aside from that everything is the about the same I just combined the function and loop together. This is just one small example of what this API can do as there is a lot more information in this API.
newer_function <-
function(weather_key,location,days){
weather_endpoint <- "http://api.weatherapi.com/v1/forecast.json?"
location <- paste("&q=",location,sep = "")
days <- paste("&days=",days,sep = "")
weather_api_url <- paste(weather_endpoint,weather_key,location,days,sep = "")
weather_api_url
newer_data <-
weather_api_url %>%
GET() %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON() %>%
use_series(forecast)%>%
use_series(forecastday)
return(newer_data)}
days <- 14
New_data <- data.frame()
for (location in c("London", "Cincinnati")) {
New_data <- newer_function(weather_key,
location,
14) %>%
mutate(city = location)%>%
## add a new column to denote which city weather is from
bind_rows(New_data) }