knitr::opts_chunk$set( fig.width = 7, fig.height = 5, eval = !(keyring::key_get('openweather_map') == ""), cache = TRUE, collapse = TRUE, comment = "#>" ) library(keyring) library(OpenWeatherPull) options(digits = 4)
OpenWeatherPull provides a complete set of R bindings to the [Open Weather Map]
(https://openweathermap.org/) RESTful API. The following functions are available to
the users.
openweather_set_key("my_api_key") - Set the required Open Weather Map API key for the session.openweather() - Fetch the weather data.openweather_request() - Send a general request to the Open Weather Map API.Objects are returned as tibbles.
# install.packages("devtools") devtools::install_github("alexhallam/OpenWeatherPull")
Load package
library(OpenWeatherPull)
To use OpenWeatherPull, you must first obtain a Open Weather Pull API
key. Once you've obtained an API key, you
can set the key as an environment variable using openweather_set_key.
openweather_set_key("abcdefghijklmnopqrstuvwxyz123456") # use API key provided by Open Weather Map
where "abcdefghijklmnopqrstuvwxyz123456" is replaced with the actual key you obtain from Open Weather Map. This will set the key for the current session.
It is best practice to keep your API key hidden. We use the keyring package,
but there are many other options described here.
The openweather() function requires an id the city id, type today, or forecast, units
devtools::install_github("alexhallam/OpenWeatherPull") keyring::key_set("openweather_map") # store API key securely provided by Open Weather Map openweather_set_key(keyring::key_get("openweather_map")) # use key_set("openweather_map") to store weather_data <- openweather(id = "5074472", # city id type = "today", # for current day's weather units = "i" # Imperial untis. ) weather_data
If you have subscribed for the paid 16 day forecast API you may also request data for future days with the below code.
openweather(id = "4560349", type = "forecast", cnt = 10, units = "i")
To know what the columns of this dataframe represent we encourage users to look at the official Open Weather Map API.
OpenWeatherPull plays nicely with tidyverse functions. We give an
example of using pmap_dfr():
If we wanted the current weather for 21 cities, as opposed to writing a openweather()
function for each request we can pass all of the parameters to pmap_dfr():
library(tidyverse) library(purrr) # a sample set of cities for current weather data city_ids <- c("4560349","4464368","4180439","4188377","4861716","5263058", "4257227","4644585","4335045", "4887398","5037649","4850751", "4281730","4553433","5074472","4684888","5454711","5780993", "5506956","5391959","5308655","5809844","4393217","4544349","4699066","5344994") type <- rep("today",length(city_ids)) cnt <- rep(14,length(city_ids)) # not used but error is thrown without it. Fix in next release units <- rep("i",length(city_ids)) pmap_dfr(list(id, type, cnt, units), openweather) #all is clouds.all = Cloudiness, %
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.