Title: | OpenWeatherMap API Wrapper |
---|---|
Description: | Accesses OpenWeatherMap's (owm) <https://openweathermap.org/> API. 'owm' itself is a service providing weather data in the past, in the future and now. Furthermore, 'owm' serves weather map layers usable in frameworks like 'leaflet'. In order to access the API, you need to sign up for an API key. There are free and paid plans. Beside functions for fetching weather data from 'owm', 'owmr' supplies tools to tidy up fetched data (for fast and simple access) and to show it on leaflet maps. |
Authors: | Stefan Kuethe [aut, cre], Amanda Dobbyn [ctb] |
Maintainer: | Stefan Kuethe <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.8.2 |
Built: | 2025-03-03 04:03:17 UTC |
Source: | https://github.com/crazycapivara/owmr |
Vectorizes function whisker.render
.
NOTE: Because whisker does not support variable names
inlcuding dots, a dot in column names is replaced by an underscore.
Therefore, you must use an underscore in the template text for
varibales including dots.
template %$$% data
template %$$% data
template |
template |
data |
data frame where column names correspond to variables names in template |
rendered template
vars <- data.frame(a = 1:3, b = 23:21) "a = {{a}} and b = {{b}}" %$$% vars
vars <- data.frame(a = 1:3, b = 23:21) "a = {{a}} and b = {{b}}" %$$% vars
Add owm tiles to leaflet map.
add_owm_tiles(map, layer_name = owm_layers$Temperature_new, ...)
add_owm_tiles(map, layer_name = owm_layers$Temperature_new, ...)
map |
leaflet map object |
layer_name |
owm layer name,
see |
... |
optional parameters passed to |
updated map object
## Not run: leaflet() %>% add_owm_tiles() %>% addMarkers(data = quakes[1:20, ]) ## End(Not run)
## Not run: leaflet() %>% add_owm_tiles() %>% addMarkers(data = quakes[1:20, ]) ## End(Not run)
Add weather data to leaflet map.
add_weather(map, data, lng = NULL, lat = NULL, icon = NULL, template = NULL, popup = NULL, ...)
add_weather(map, data, lng = NULL, lat = NULL, icon = NULL, template = NULL, popup = NULL, ...)
map |
|
data |
owm data |
lng |
numeric vector of longitudes
(if |
lat |
numeric vector of latitudes
(if |
icon |
vector of owm icon names (usually included in weather column of owm data) |
template |
template in the form of |
popup |
vector containing (HTML) content for popups,
skipped in case parameter |
... |
see |
updated map object
## Not run: owm_data <- find_city("Malaga", units = "metric") %>% owmr_as_tibble() map <- leaflet() %>% addTiles() %>% add_weather( owm_data, template = "<b>{{name}}</b>, {{temp}}°C", icon = owm_data$weather_icon ) ## End(Not run)
## Not run: owm_data <- find_city("Malaga", units = "metric") %>% owmr_as_tibble() map <- leaflet() %>% addTiles() %>% add_weather( owm_data, template = "<b>{{name}}</b>, {{temp}}°C", icon = owm_data$weather_icon ) ## End(Not run)
Flatten weather column in data frame. (DEPRECATED)
cbind_weather(data)
cbind_weather(data)
data |
data frame containing weather column |
data frame with flattened weather (data)
## Not run: get_forecast("Kassel") %>% cbind_weather() ## End(Not run)
## Not run: get_forecast("Kassel") %>% cbind_weather() ## End(Not run)
Get current weather data for a number of cities within a given bounding box.
find_cities_by_bbox(bbox = c(12, 32, 15, 37, 10), ...)
find_cities_by_bbox(bbox = c(12, 32, 15, 37, 10), ...)
bbox |
bounding box, numric vector of the form (lon-left, lat-bottom, lon-right, lat-top, zoom) |
... |
Get current weather data for a number of cities around a given geo point.
find_cities_by_geo_point(lat, lon, cnt = 3, ...)
find_cities_by_geo_point(lat, lon, cnt = 3, ...)
lat |
latitude of geo point |
lon |
longitude of geo point |
cnt |
number of cities |
... |
see owm api documentation |
list
## Not run: find_cities_by_geo_point(lat = 51.50853, lon = -0.12574, cnt = 5) ## End(Not run)
## Not run: find_cities_by_geo_point(lat = 51.50853, lon = -0.12574, cnt = 5) ## End(Not run)
Either search for city by name or fetch weather data for a number of cities around geo point.
find_city(city = NA, ...)
find_city(city = NA, ...)
city |
city name (and country code) |
... |
see owm api documentation, pass |
list of weather data for matches
## Not run: find_city("London,UK") find_city(lat = 51.50853, lon = -0.12574, cnt = 5) ## End(Not run)
## Not run: find_city("London,UK") find_city(lat = 51.50853, lon = -0.12574, cnt = 5) ## End(Not run)
Flatten list. (DEPRECATED)
flatten(data)
flatten(data)
data |
list returned from owm |
flattened list
## Not run: get_current("Rio de Janeiro") %>% flatten() get_current("Rio de Janeiro") %>% flatten() %>% tidy_up_() ## End(Not run)
## Not run: get_current("Rio de Janeiro") %>% flatten() get_current("Rio de Janeiro") %>% flatten() %>% tidy_up_() ## End(Not run)
Parse weather column to (single) data frame. (DEPRECATED)
flatten_weather(x)
flatten_weather(x)
x |
weather column (NOT name) |
data frame
## Not run: result <- get_forecast("Kassel", units = "metric")$list weather <- flatten_weather(result$weather) weather$description %>% print() ## End(Not run)
## Not run: result <- get_forecast("Kassel", units = "metric")$list weather <- flatten_weather(result$weather) weather$description %>% print() ## End(Not run)
Get current weather data for given city.
get_current(city = NA, ...)
get_current(city = NA, ...)
city |
city name or id |
... |
see owm api documentation, you can also skip parameter
|
list
## Not run: get_current("London", units = "metric") get_current(2643741, lang = "DE") get_current(lon = -0.09184, lat = 51.51279) get_current(zip = "94040,US") ## End(Not run)
## Not run: get_current("London", units = "metric") get_current(2643741, lang = "DE") get_current(lon = -0.09184, lat = 51.51279) get_current(zip = "94040,US") ## End(Not run)
Get current weather data for multiple cities.
get_current_for_group(city_ids, ...)
get_current_for_group(city_ids, ...)
city_ids |
numeric vector containing city ids |
... |
see owm api documentation |
list
owm_cities
dataset in order to
lookup city ids
## Not run: city_ids = c(2831088, 2847639, 2873291) result <- get_current_for_group(city_ids) result$cnt == nrow(result$list) weather_frame <- result$list ## End(Not run)
## Not run: city_ids = c(2831088, 2847639, 2873291) result <- get_current_for_group(city_ids) result$cnt == nrow(result$list) weather_frame <- result$list ## End(Not run)
Get 3h forecast data.
get_forecast(city = NA, ...)
get_forecast(city = NA, ...)
city |
city name or id |
... |
see owm api documentation, you can also skip parameter
|
list
## Not run: result <- get_forecast("Kassel", units = "metric") names(result) get_forecast("London", cnt = 10) get_forecast(lat = -22.90278, lon = -22.90278, cnt = 3, units = "metric") ## End(Not run)
## Not run: result <- get_forecast("Kassel", units = "metric") names(result) get_forecast("London", cnt = 10) get_forecast(lat = -22.90278, lon = -22.90278, cnt = 3, units = "metric") ## End(Not run)
Get daily forecast data up to 16 days.
get_forecast_daily(city = NA, ...)
get_forecast_daily(city = NA, ...)
city |
city name or id |
... |
see owm api documentation, you can also skip parameter
|
list
## Not run: # 9 day forecast result <- get_forecast_daily("London", cnt = 9) forecast_frame <- result$list ## End(Not run)
## Not run: # 9 day forecast result <- get_forecast_daily("London", cnt = 9) forecast_frame <- result$list ## End(Not run)
Get icon url.
get_icon_url(icon)
get_icon_url(icon)
icon |
icon name as returned by owm |
icon url
## Not run: forecast <- get_forecast("London")$list weather <- flatten_weather(forecast$weather) icons <- get_icon_url(weather$icon) ## End(Not run)
## Not run: forecast <- get_forecast("London")$list weather <- flatten_weather(forecast$weather) icons <- get_icon_url(weather$icon) ## End(Not run)
A dataset containing city ids and coordinates to be used in queries.
owm_cities
owm_cities
data frame with 74071 rows and 4 variables:
city id
city name
latitude
longitude
two letter country code
http://bulk.openweathermap.org/sample/city.list.json.gz
List of available owm weather map layers.
owm_layers
owm_layers
An object of class list
of length 16.
https://openweathermap.org/api/weathermaps
In order to access the API, you need to sign up for an API key
at https://openweathermap.org/.
For optional parameters (...
) in functions see
https://openweathermap.org/api/
## Not run: # first of all you have to set up your api key owmr_settings("your_api_key") # or store it in an environment variable called OWM_API_KEY (recommended) Sys.setenv(OWM_API_KEY = "your_api_key") # if not set globally # get current weather data for "Kassel" with temperatures in °C get_current("Kassel", units = "metric") # get 3h forcast data (7 rows) get_forecast("London", cnt = 7) # ... ## End(Not run)
## Not run: # first of all you have to set up your api key owmr_settings("your_api_key") # or store it in an environment variable called OWM_API_KEY (recommended) Sys.setenv(OWM_API_KEY = "your_api_key") # if not set globally # get current weather data for "Kassel" with temperatures in °C get_current("Kassel", units = "metric") # get 3h forcast data (7 rows) get_forecast("London", cnt = 7) # ... ## End(Not run)
Parse owmr response to tibble.
owmr_as_tibble(resp, simplify = TRUE) ## S3 method for class 'owmr_weather' owmr_as_tibble(resp, simplify = TRUE) ## Default S3 method: owmr_as_tibble(resp, simplify = TRUE) ## S3 method for class 'owmr_forecast_daily' owmr_as_tibble(resp, simplify = TRUE)
owmr_as_tibble(resp, simplify = TRUE) ## S3 method for class 'owmr_weather' owmr_as_tibble(resp, simplify = TRUE) ## Default S3 method: owmr_as_tibble(resp, simplify = TRUE) ## S3 method for class 'owmr_forecast_daily' owmr_as_tibble(resp, simplify = TRUE)
resp |
response object returned from functions like
|
simplify |
return tibble only? |
list containing tibble or tibble only (simplify = TRUE
)
Set api key. Internally it calls Sys.setenv
to store the api key in an environment variable called OWM_API_KEY
.
owmr_settings(api_key)
owmr_settings(api_key)
api_key |
owm api key |
## Not run: owmr_settings(api_key = "your-api-key") ## End(Not run)
## Not run: owmr_settings(api_key = "your-api-key") ## End(Not run)
Apply functions to columns.
parse_columns(data, functions_)
parse_columns(data, functions_)
data |
data frame |
functions_ |
named list where keys correspond to column names |
updated data frame
## Not run: parse_dt <- function(x){as.POSIXct(x, origin = "1970-01-01")} forecast <- get_forecast("Kassel")$list forecast %<>% parse_columns(list(dt = parse_dt)) ## End(Not run)
## Not run: parse_dt <- function(x){as.POSIXct(x, origin = "1970-01-01")} forecast <- get_forecast("Kassel")$list forecast %<>% parse_columns(list(dt = parse_dt)) ## End(Not run)
Remove prefices from column names.
remove_prefix(data, prefices, sep = ".")
remove_prefix(data, prefices, sep = ".")
data |
data frame |
prefices |
vector of prefices to be removed from column names |
sep |
prefix separator |
data frame with updated column names
x <- data.frame(main.temp = 1:10, sys.msg = "OK", cnt = 10:1) names(x) remove_prefix(x, c("main", "sys")) %>% names()
x <- data.frame(main.temp = 1:10, sys.msg = "OK", cnt = 10:1) names(x) remove_prefix(x, c("main", "sys")) %>% names()
Search owm_cities
dataset by city name
and country code.
search_city_list(city, country_code = "")
search_city_list(city, country_code = "")
city |
city name (regex) |
country_code |
two letter country code (AU, DE, ...),
use |
data frame with matches
owm_cities
dataset
search_city_list("London", "GB") search_city_list("London") search_city_list("Lond")
search_city_list("London", "GB") search_city_list("London") search_city_list("Lond")
Calls tidy_up_
passing data$list
as data
argument.
tidy_up(data, ...)
tidy_up(data, ...)
data |
result returned from owm
containing data frame in |
... |
see |
data with updated data frame (data$list
)
## Not run: get_forecast("London") %>% tidy_up() ## End(Not run)
## Not run: get_forecast("London") %>% tidy_up() ## End(Not run)
Tidy up owm data. (DEPRECATED)
tidy_up_(data, flatten_weather_ = TRUE, use_underscore_ = TRUE, remove_prefix_ = c("main", "sys"))
tidy_up_(data, flatten_weather_ = TRUE, use_underscore_ = TRUE, remove_prefix_ = c("main", "sys"))
data |
data frame |
flatten_weather_ |
see |
use_underscore_ |
substitute dots in column names with underscores |
remove_prefix_ |
prefices to be removed for shorter column names
( |
updated data frame
tidy_up
,remove_prefix
,use_underscore
## Not run: result <- find_city("Malaga") result$list %>% tidy_up_() # keep dots in column names result$list %>% tidy_up_(use_underscore_ = FALSE) # keep all prefices result$list %>% tidy_up_(remove_prefix_ = NULL) ## End(Not run)
## Not run: result <- find_city("Malaga") result$list %>% tidy_up_() # keep dots in column names result$list %>% tidy_up_(use_underscore_ = FALSE) # keep all prefices result$list %>% tidy_up_(remove_prefix_ = NULL) ## End(Not run)
Substitute dots in column names with underscores.
use_underscore(data)
use_underscore(data)
data |
data frame |
data frame with updated column names
names(airquality) use_underscore(airquality) %>% names()
names(airquality) use_underscore(airquality) %>% names()