Installation

# stable
install.packages("omwr")

# unstable
remotes::install_github("crazycapivara/owmr")

Setup

First of all you have to store your API key in an environment variable called OWM_API_KEY. For the current session this can be done with:

Sys.setenv(OWM_API_KEY = "yourSuperSecretApiKey")

Get data

Due to the ... parameter in R you can pass all parameters defined in the openweathermap-api-reference to the functions fetching data.

library(owmr)

current <- get_current("Kassel", units = "metric", lang = "de")
class(current)
## [1] "list"         "owmr_weather"
forecast <- get_forecast("Malaga", units = "metric")
class(forecast)
## [1] "list"          "owmr_forecast"
(nyc <- search_city_list("New York City"))
##            id            nm      lat       lon countryCode
## 32214 5128581 New York City 40.71427 -74.00597          US
current_multiple <- find_cities_by_geo_point(
  nyc$lat,
  nyc$lon,
  cnt = 5,
  units = "metric"
)
class(current_multiple)
## [1] "list"      "owmr_find"
bbox_greater_london <- c(-0.489, 51.28, 0.236, 51.686, 10)
current_greater_london <- find_cities_by_bbox(bbox_greater_london)
class(current_greater_london)
## [1] "list"          "owmr_box_city"

Get similar-ish data frames with owmr_as_tibble

columns <- c("dt_txt", "temp","pressure", "humidity" ,"temp_min", "temp_max")

current %>% owmr_as_tibble() %>%
  .[, columns]
## # A tibble: 1 x 6
##   dt_txt               temp pressure humidity temp_min temp_max
##   <chr>               <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
## 1 2019-12-12 07:20:02  1.81     1002       95    -0.56     4.44
forecast %>% owmr_as_tibble() %>%
  .[1:4, columns]
## # A tibble: 4 x 6
##   dt_txt               temp pressure humidity temp_min temp_max
##   <chr>               <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
## 1 2019-12-12 09:00:00  37.9     1015        9     37.9     38.1
## 2 2019-12-12 12:00:00  32.0     1016       16     32.0     32.2
## 3 2019-12-12 15:00:00  28.7     1017       14     28.7     28.8
## 4 2019-12-12 18:00:00  28.1     1015       16     28.1     28.2
current_multiple %>% owmr_as_tibble() %>%
  .[, c(columns, "name")]
## # A tibble: 5 x 7
##   dt_txt               temp pressure humidity temp_min temp_max name            
##   <chr>               <dbl>    <dbl>    <dbl>    <dbl>    <dbl> <chr>           
## 1 2019-12-12 07:17:29 -1.76     1035       54    -4.44     0.56 New York        
## 2 2019-12-12 07:17:25 -1.81     1035       54    -4.44     0.56 Hoboken         
## 3 2019-12-12 07:17:25 -1.88     1035       54    -4.44     0.56 Hudson County   
## 4 2019-12-12 07:17:25 -1.89     1035       54    -4.44     0.56 Jersey City     
## 5 2019-12-12 07:17:29 -1.75     1035       54    -4.44     0.56 Long Island City
current_greater_london %>% owmr_as_tibble() %>%
  .[, c(columns, "name")]
## # A tibble: 5 x 7
##   dt_txt            temp pressure humidity temp_min temp_max name               
##   <chr>            <dbl>    <dbl>    <dbl>    <dbl>    <dbl> <chr>              
## 1 2019-12-12 07:0…  1.16     1000       86    -0.56     3    City of London     
## 2 2019-12-12 07:1…  1.27      999       86    -1.11     3.33 Blackheath         
## 3 2019-12-12 07:1…  1.43      999      100    -0.56     3.33 London Borough of …
## 4 2019-12-12 07:0…  0.78     1000       74    -1.11     2.22 Dagenham           
## 5 2019-12-12 07:0…  1.34     1000      100     0        3    Watford

Leaflet integration

rio <- search_city_list("Rio de Janeiro")

# Get data for 9 cities around Rio
owm_data <- find_cities_by_geo_point(
  lat = rio$lat,
  lon = rio$lon,
  cnt = 9,
  units = "metric"
) %>% owmr_as_tibble()

owm_data %<>% parse_columns(list(temp = round))
head(owm_data)
## # A tibble: 6 x 22
##   dt_txt  temp pressure humidity temp_min temp_max weather_id weather_main
##   <chr>  <dbl>    <dbl>    <dbl>    <dbl>    <dbl>      <dbl> <chr>       
## 1 2019-…    22     1008       94     20         23        300 Drizzle     
## 2 2019-…    22     1008       94     19.4       23        300 Drizzle     
## 3 2019-…    22     1008       94     19.4       23        300 Drizzle     
## 4 2019-…    22     1007       94     20         23        500 Rain        
## 5 2019-…    22     1008       94     20         23        500 Rain        
## 6 2019-…    22     1008       94     20         23        300 Drizzle     
## # … with 14 more variables: weather_description <chr>, weather_icon <chr>,
## #   wind_speed <dbl>, wind_deg <dbl>, clouds_all <dbl>, id <int>, name <chr>,
## #   dt <int>, rain <lgl>, snow <lgl>, coord_lat <dbl>, coord_lon <dbl>,
## #   feels_like <dbl>, sys_country <chr>
# Create a popup template
popup_tpl <- paste0(
  "<b>{{name}}</b></br>",
  "{{coord_lon}}, {{coord_lat}}</br>",
  "{{temp}}°C, ",
  "<i>{{weather_description}}</i>"
)

# Test it ...
popup_tpl %$$% owm_data %>% head(2)
## [1] "<b>Rio de Janeiro</b></br>-43.21, -22.9</br>22°C, <i>light intensity drizzle</i>"
## [2] "<b>São Cristóvão</b></br>-43.23, -22.9</br>22°C, <i>light intensity drizzle</i>"
library(leaflet)

leaflet(width = "100%") %>%
  addProviderTiles("CartoDB.DarkMatter") %>%
  add_weather(owm_data, icon = owm_data$weather_icon, template = popup_tpl)

Furthermore, you can add weather map layers from OpenWeatherMap to the map object via add_owm_tiles. For available layers see owm_layers.