R/history.R

Defines functions weatherstack_historical

Documented in weatherstack_historical

#' Historical Weather data
#'
#' @param key2 Your API access key, which can be found in your account dashboard.
#' @param location Use this parameter to pass a single location or multiple semicolon-separated location identifiers to the API.
#' @param date pass one historical date or multiple semicolon-separated dates to the API.
#' @param unit Use this parameter to pass one of "Farenheit, Celcius"
#'
#' @return JSON dataframe
#' @export
#' @import httr
#' @import jsonlite
#'
#' @examples
#' weatherstack_historical("ba435151893f4b833c9b27ca6f28044f", "Vancouver", "2015-01-02", 'Celcius')
weatherstack_historical <- function(key2, location, date, unit){
  # checking unit input
  if (unit == 'Fahrenheit'){
    units = 'f'
  }else{
    units = 'm'
    print("Using metric units")
  }
  # set up for the request of the url and path from website
  domain <- "http://api.weatherstack.com/"
  api_function <- "historical"
  #parameters required for the API function
  params <- list(access_key = key2,
                 query = location,
                 historical_date = date,
                 units = units)
  #url combine with path and parameters
  url <- modify_url(paste(domain, api_function, sep = ""),
                    query = params)
  #encoding data as json
  resp <- POST(url, encode = 'json')
  # 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)}
  # manipulating json to display as dataframe
  resp_json <- fromJSON(content(resp, "text"),
                        flatten = TRUE)
  df<- as.data.frame(resp_json)
  df_t <- t(df)
  return(df_t)
}
Yuening-Li/weatherstack documentation built on Feb. 16, 2020, 5:42 a.m.