R/weather.R

Defines functions weather

Documented in weather

#' Weather
#' 
#' Returns a list of the weather information given by \url{https://www.weatherstack.com/}
#' 
#' @param city object with the class "character", default city is "Linkoping", Use only ASCII characters.
#' @return A list of the weather in the city wanted. 
#' @source \url{https://www.weatherstack.com/}
#' @importFrom jsonlite fromJSON
#' @example 
#' weather("Linkoping")
#' @export


weather<-function(city="linkoping"){
  stopifnot(class(city)=="character")
  
  url_start<-"http://api.weatherstack.com/current"
  # My API key
  my_key<-"5c074fdd5f5a71450a7b3f06c60a9b38"
  
  city<-base::eval(city)
  get_from_url<-base::paste(url_start,"&access_key=",my_key,"&query=",city,sep = "")
 
    weather<-jsonlite::fromJSON(get_from_url)
   if(length(weather) <3){
     text<-cat(city,"is not found.")
     
   }else{

    class_change <- function(data){
      base::structure(data, class = "weather")
    }
    return(class_change(weather))
   }
}
harjew/lab5G3 documentation built on Nov. 4, 2019, 1:28 p.m.