OpenWeatherMap API Wrapper

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.


CRAN_Status_Badge

owmr accesses OpenWeatherMap's API, a service providing weather data in the past, in the future and now and furthermore, serving weather map layers usable in frameworks like leaflet. In order to access its API you have to sign up for an API key at

Builds

master

Travis-CI Build Status

develop

Travis-CI Build Status

Installation

install.packages("owmr")
 
# unstable
devtools::install_github("crazycapivara/owmr")
 
# bleeding edge
devtools::install_github("crazycapivara/owmr", ref = "develop")

Introduction

See OpenWeatherMap's API documentation for optional parameters, which can be passed to all functions fetching weather data via the ... parameter in R

Setup

library(owmr)
 
# 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

Usage

# get current weather data by city name
(res <- get_current("London", units = "metric") %>%
  owmr_as_tibble()) %>% names()
##  [1] "dt_txt"              "temp"                "pressure"           
##  [4] "humidity"            "temp_min"            "temp_max"           
##  [7] "weather_id"          "weather_main"        "weather_description"
## [10] "weather_icon"        "wind_speed"          "wind_deg"           
## [13] "clouds_all"          "dt_sunrise_txt"      "dt_sunset_txt"
res[, 1:6]
## # A tibble: 1 x 6
##   dt_txt               temp pressure humidity temp_min temp_max
##   <chr>               <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
## 1 2018-10-28 20:50:00  4.22     1017       81        3        6
# ... by city id
(rio <- search_city_list("Rio de Janeiro")) %>%
  as.list()
## $id
## [1] 3451190
## 
## $nm
## [1] "Rio de Janeiro"
## 
## $lat
## [1] -22.90278
## 
## $lon
## [1] -43.2075
## 
## $countryCode
## [1] "BR"
get_current(rio$id, units = "metric") %>%
  owmr_as_tibble() %>% .[, 1:6]
## # A tibble: 1 x 6
##   dt_txt               temp pressure humidity temp_min temp_max
##   <chr>               <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
## 1 2018-10-28 21:00:00  23.2     1014       64       23       24
# get current weather data for cities around geo point
res <- find_cities_by_geo_point(
  lat = rio$lat,
  lon = rio$lon,
  cnt = 5,
  units = "metric"
) %>% owmr_as_tibble()
 
idx <- c(names(res[1:6]), "name")
res[, idx]
## # A tibble: 5 x 7
##   dt_txt            temp pressure humidity temp_min temp_max name         
##   <chr>            <dbl>    <dbl>    <dbl>    <dbl>    <dbl> <chr>        
## 1 2018-10-28 21:0…  23.2     1014       64       23       24 Rio de Janei…
## 2 2018-10-28 21:0…  23.2     1014       64       23       24 São Cristóvão
## 3 2018-10-28 21:0…  23.2     1014       69       23       24 Botafogo     
## 4 2018-10-28 21:0…  23.2     1014       69       23       24 Pavão-Pavaoz…
## 5 2018-10-28 21:0…  23.2     1014       64       23       24 Vila Joaniza
# get forecast
forecast <- get_forecast("London", units = "metric") %>%
  owmr_as_tibble()
 
forecast[, 1:6]
## # A tibble: 40 x 6
##    dt_txt               temp pressure humidity temp_min temp_max
##    <chr>               <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
##  1 2018-10-28 21:00:00  2.76    1023.       71     2.76     4.22
##  2 2018-10-29 00:00:00  1.64    1021.       90     1.64     2.73
##  3 2018-10-29 03:00:00  2.54    1019.       99     2.54     3.27
##  4 2018-10-29 06:00:00  1.64    1018.       94     1.64     2   
##  5 2018-10-29 09:00:00  4.65    1016.       84     4.65     4.65
##  6 2018-10-29 12:00:00  8.44    1013.       77     8.44     8.44
##  7 2018-10-29 15:00:00  8.47    1010.       70     8.47     8.47
##  8 2018-10-29 18:00:00  5.96    1008.       76     5.96     5.96
##  9 2018-10-29 21:00:00  3.57    1005.       87     3.57     3.57
## 10 2018-10-30 00:00:00  3.07    1003.       97     3.07     3.07
## # ... with 30 more rows
# apply funcs to some columns
funcs <- list(
  temp = round,
  wind_speed = round
)
forecast %<>% parse_columns(funcs)
 
# do some templating ...
("{{dt_txt}}h {{temp}}°C, {{wind_speed}} m/s" %$$%
  forecast) %>% head(10)
##  [1] "2018-10-28 21:00:00h 3°C, 5 m/s" "2018-10-29 00:00:00h 2°C, 4 m/s"
##  [3] "2018-10-29 03:00:00h 3°C, 4 m/s" "2018-10-29 06:00:00h 2°C, 4 m/s"
##  [5] "2018-10-29 09:00:00h 5°C, 3 m/s" "2018-10-29 12:00:00h 8°C, 4 m/s"
##  [7] "2018-10-29 15:00:00h 8°C, 5 m/s" "2018-10-29 18:00:00h 6°C, 4 m/s"
##  [9] "2018-10-29 21:00:00h 4°C, 4 m/s" "2018-10-30 00:00:00h 3°C, 4 m/s"

Documentation

or type

?owmr

Run tests

devtools::test()
## Loading owmr

## owmr 0.7.4
##    another crazy way to talk to OpenWeatherMap's API
##    Documentation: type ?owmr or https://crazycapivara.github.io/owmr/
##    Issues, notes and bleeding edge: https://github.com/crazycapivara/owmr/

## Testing owmr

## city list: ..
## mock httr::GET current: ....
## current weather data for multiple cities: ......
## current weather data: ........
## _DEPRECATED: W.
## mock httr::GET forecast: ......
## parse columns: ..
## render operator: ...
## tidy up data: ...
## 
## Warnings ------------------------------------------------------------------
## 1. tidy up all (@test_deprecated.R#8) - 'tidy_up_' is deprecated.
## Use 'owmr_as_tibble' instead.
## See help("Deprecated")
## 
## DONE ======================================================================

News

owmr 0.8.1

  • Added function find_cities_by_bbox to get current weather data for a number of cities within a given bounding box

owmr 0.8.0

  • Added generic function owmr_as_tibble to get similiar-ish and tidy data frames for all kind of responses
  • Removed find_stations_by_geo_point and get_current_from_station because of a new owm stations api

owmr 0.7.4

  • Fixed bug in add_owm_tiles
  • find_stations_by_geo_point and get_current_from_station are not supported anymore because of breaking changes in the owm api for stations. Functions will be removed in a future version.

owmr 0.7.3

  • Added feature to store the api key in an environment variable called OWM_API_KEY
  • It is still possible to set the key via owmr_settings, but it is recommended that you use the environment variable instead

owmr 0.7.2

  • Updated package description

owmr 0.7.1

  • Fixed false poitives in check for undefined global variable . from magrittr

owmr 0.7.0

  • Added functions to add owm tiles to leaflet map.
  • Note: Performance of owm tile server seems to be low.

owmr 0.6.1

  • Added function to show weather data on leaflet map.
    • Added operator %$$% to render template text for popups.

owmr 0.5.0

  • Refactored code
  • Added function to fetch current weather data for cities around geo point (more or less an alias to find_city).
  • Marked UTF-8 string in data fixed.

owmr 0.4.1

  • Added function to fetch current weather data for multiple cities at once.

owmr 0.4.0

  • Added function to search owm's city list by city name.
  • Added tests for current data.
  • Added helper function to fetch 'fresh' data used in tests.

owmr 0.3.0

  • Added a NEWS.md file to track changes to the package.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("owmr")

0.8.1 by Stefan Kuethe, 4 months ago


https://github.com/crazycapivara/owmr/, https://crazycapivara.github.io/owmr/


Report a bug at https://github.com/crazycapivara/owmr/issues/


Browse source code at https://github.com/cran/owmr


Authors: Stefan Kuethe [aut, cre] , Amanda Dobbyn [ctb]


Documentation:   PDF Manual  


Task views: Web Technologies and Services


MIT + file LICENSE license


Imports magrittr, httr, jsonlite, plyr, tibble, tidyr

Suggests leaflet, whisker, testthat, covr


See at CRAN