#' Historical time-series results of weather data
#'
#' @param api_key Your API access key, which can be found in your acccount dashboard.
#' @param location pass a single location
#' @param historical_date_start a start date for the current historical time-series request.
#' @param historical_date_end an end date for the current historical time-series request.
#' @return datframe contain time series weather information
#' @export
#' @import jsonlite
#' @import httr
#' @import stringr
#' @examples
#' weatherstack_timeSeries("ba435151893f4b833c9b27ca6f28044f","New York","2015-01-21","2015-01-25")
weatherstack_timeSeries <- function(api_key,location,historical_date_start,historical_date_end) {
# setting up the url to access the api
domain<- "http://api.weatherstack.com/"
endpoint <- "historical"
params<-list(access_key =api_key,
query=location,
historical_date_start=historical_date_start,
historical_date_end=historical_date_end)
url <- modify_url(paste(domain,endpoint,sep =""),
query = params)
#post request api
resp<- POST(url)
# try for proper API key and access
if(resp$status_code==101){
print("User supplied an invalid access key.")
return (NULL)}
else if(resp$status_code==404){
print("User requested a resource which does not exist.")
return (NULL)}
#convert the data to json format
resp_json <- fromJSON(content(resp, "text"),
flatten = TRUE)
#extract historical data from the json, and make it readable format in dataframe
count <- as.numeric(str_sub(historical_date_end,-2)) - as.numeric(str_sub(historical_date_start,-2))
hist <-(resp_json[4])$historical
new = data.frame()
for(i in 1:count){
df <- as.data.frame(hist[[i]])
new <- rbind(new, df)
}
#return readable dataframe
return(new)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.