You are almost there. There are a couple of things needed to iterate the get_forecast_for
function by rows. From the purrr
package, the pmap
function is good for repeating a function by row whereas the imap
function can be used for repeating a function by cells in a row.
Using this approach, I wrote two functions: weather_at_coords
and weather
. weather_at_coords
is used to send a request to DarkSkyAPI for weather at specific location in a given time range (i.e., last ten days). The weather
function is used to repeat the function by row.
I saw that you wanted the nested object daily
, so wrote the function to extract that list from the response. I'm assuming that you also wanted the results in a data.frame so I added bind_rows
. I added a column id
so that rows can be properly linked to a location (or you can add any columns that you like).
# pkgs
library(tidyverse)
library(darksky)
# set API Key: free from https://darksky.net/dev
darksky::darksky_api_key()
# Forecast at a given point and time period
weather_at_coords <- function(...) {
d <- rlang::list2(...)
time <- seq(Sys.Date()-10, Sys.Date(), "1 day")
response <- imap(time, ~ darksky::get_forecast_for(d$lat, d$lon, .x, units = "si")[["daily"]])
out <- bind_rows(response) %>% mutate(id = d$id)
return(out)
}
# primary function (iterates across rows)
weather <- function(data) {
result <- pmap(data, ~ weather_at_coords(...))
return(bind_rows(result))
}
# sample data
d <- data.frame(
id = c("a", "b"),
lat = c(37.8267,34.8267),
lon = c(-122.423, -120.423)
)
# run
x <- weather(d)
x
Notes
- Make sure you have the
rlang
package installed
- Adjust the
lat
and lon
variable names as required.