Open Weather API

You can use the Open Weather API to look up anything about weather data. I decided to use the current weather data, where you can look up any city in the world and get that cities current weather. You can get the basic information like temperature and the sky, or you van get advanced information like the humidity and wind speed.

Get YourAPI Key

First go to https://home.openweathermap.org/api_keys and create an account to get your own API key. Once you have made your account you will have your own API key.

Set Up Your API

This is the script you can use to make a call to your API. In this script I was able to receive the current weather in Cincinnati, OH. You must put your own API key in at step 2 in order to receive data.

# Load these necessary packages
library(httr)     
library(jsonlite)  
library(tibble)  
library(dplyr) 

# STEP 2: Get API key

api_key <- "Your API Key"  

# STEP 3: Define the location you want to look up the weather

city_name <- "Cincinnati"  
country_code <- "US"  

# STEP 4: Build the URL

base_url <- "https://api.openweathermap.org/data/2.5/weather"
full_url <- paste0(base_url,
                   "?q=", city_name, ",", country_code,
                   "&units=imperial", 
                   "&appid=", api_key)

# STEP 5: Make the API request
response <- GET(full_url)

# STEP 6: Parse the JSON content
weather_data <- fromJSON(content(response, as = "text"))

# STEP 7: Create the data frame
weather_df <- data.frame(
  city = weather_data$name,
  country = weather_data$sys$country,
  temperature = weather_data$main$temp,
  feels_like = weather_data$main$feels_like,
  humidity = weather_data$main$humidity,
  pressure = weather_data$main$pressure,
  weather_main = weather_data$weather$main,  
  weather_description = weather_data$weather$description,  
  wind_speed_mps = weather_data$wind$speed,
  datetime = as.POSIXct(weather_data$dt, origin = "1970-01-01", tz = "UTC")
)

print(weather_df)