Little useless-useful R functions – Is it raining yet?

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Summer. Sunny weather. Vitamin D. And if you are missing vitamin Rain because you are growing a garden or simply want an inner cooling, hit this useless function to see, if there is a rain anywhere to be seen.

Besides looking on the phone, on the web, or sticking the head out of the window to check the weather, a useless way to do it, is to write function that will tell you just that. I know, oh… the absurdity. Nevertheless, the function is just one of many possibilities:

isItRainingYet <- function(city){
  
  API_key = "5xxxxeexxxxxcbyyyy8xxxxxexxxxxxa1" #masked!
  City_name = city
  
  api <- paste0("http://api.openweathermap.org/data/2.5/weather?q=",City_name,"&appid=",API_key)
  res <- fromJSON(api)
  perc <- res$clouds$all[1]
  perc <- as.integer(perc)
  
  rain <- " "
  if (perc<=20) rain <-'Meeh'
  if (perc>20 & perc<=60) rain <- 'Huuuh, but still meeh'
  if (perc>60 & perc<=85) rain <- 'Looking better'
  if (perc>85) rain <- 'Bring it on!'


  return(rain)
    
}

And you run the function with city name:

isItRainingYet("Ljubljana")

With current weather in my city, function returns “Meeh” ? Which is no surprise at all.

I could be using also a forecast API function to check for example how many days until the next rain or will there be enough rain that I would not need to water my garden. And many many wacky ideas.

Couple of words about the API. OpenWeatherMap is API weather service which requires a registration (email and name) in order to receive an API_key. Just paste it in the variable and you are good to go (in less than a minute). Documentation for Rest API is available at the website, as well as the limitation of usage. Check your limits for free usage, or if you are coming from enterprise check their offerings.

As always, code is available in at the Github in same Useless_R_function repository.

Happy R-coding!

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)