It doesn’t get much better for me than when I can combine R and weather data in new ways. I’ve got something brewing with my Nest thermostat and needed to get some current wx readings plus forecast data. I could have chosen a number of different sources or API’s but I wanted to play with the data over at forecast.io (if you haven’t loaded their free weather “app” on your phone/tablet you should do that NOW) so I whipped together a small R package to fetch and process the JSON to make it easier to work with in R.
The package contains a singular function and the magic is all in the conversion of the JSON hourly/minutely weather data into R data frames, which is dirt simple to do since RJSONIO
and sapply
do all the hard work for us:
# take the JSON blob we got from forecast.io and make an R list from it fio <- fromJSON(fio.json) # extract hourly forecast data fio.hourly.df <- data.frame( time = ISOdatetime(1960,1,1,0,0,0) + sapply(fio$hourly$data,"[[","time"), summary = sapply(fio$hourly$data,"[[","summary"), icon = sapply(fio$hourly$data,"[[","icon"), precipIntensity = sapply(fio$hourly$data,"[[","precipIntensity"), temperature = sapply(fio$hourly$data,"[[","temperature"), apparentTemperature = sapply(fio$hourly$data,"[[","apparentTemperature"), dewPoint = sapply(fio$hourly$data,"[[","dewPoint"), windSpeed = sapply(fio$hourly$data,"[[","windSpeed"), windBearing = sapply(fio$hourly$data,"[[","windBearing"), cloudCover = sapply(fio$hourly$data,"[[","cloudCover"), humidity = sapply(fio$hourly$data,"[[","humidity"), pressure = sapply(fio$hourly$data,"[[","pressure"), visibility = sapply(fio$hourly$data,"[[","visibility"), ozone = sapply(fio$hourly$data,"[[","ozone") )
You can view the full code over at github and there’s some sample usage below.
library("devtools") install_github("Rforecastio", "hrbrmstr") library(Rforecastio) library(ggplot2) # NEVER put credentials or api keys in script bodies or github repos!! # the "config" file has one thing in it, the api key string on one line # this is all it takes to read it in fio.api.key = readLines("~/.forecast.io") my.latitude = "43.2673" my.longitude = "-70.8618" fio.list <- fio.forecast(fio.api.key, my.latitude, my.longitude) # setup "forecast" highlight plot area forecast.x.min <- ISOdatetime(1960,1,1,0,0,0) + unclass(Sys.time()) forecast.x.max <- max(fio.list$hourly.df$time) if (forecast.x.min > forecast.x.max) forecast.x.min <- forecast.x.max fio.forecast.range.df <- data.frame(xmin=forecast.x.min, xmax=forecast.x.max, ymin=-Inf, ymax=+Inf) # plot the readings fio.gg <- ggplot(data=fio.list$hourly.df,aes(x=time, y=temperature)) fio.gg <- fio.gg + labs(y="Readings", x="Time") fio.gg <- fio.gg + geom_rect(data=fio.forecast.range.df, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill="yellow", alpha=(0.15), inherit.aes = FALSE) fio.gg <- fio.gg + geom_line(aes(y=humidity*100), color="green") fio.gg <- fio.gg + geom_line(aes(y=temperature), color="red") fio.gg <- fio.gg + geom_line(aes(y=dewPoint), color="blue") fio.gg <- fio.gg + theme_bw() fio.gg
15 Comments
This is great. I can’t wait to pair this with rCharts and d3. I somehow missed the APi with forecast.io. Thanks.
Nice work! Have you considered using roxygen2 for package documentation (http://adv-r.had.co.nz/Documenting-functions.html#roxygen-process)? Then you wouldn’t have to write man files manually.
Thx for the tip about roxygen2. Just configured it in my local RStudio Desktop instance. Trying to figure out why my various RStudio Server installs aren’t giving me the same option in the build setup dialog.
Never put the latitude and longitude of your home in script bodies / Github repos either. ;-)
:-)
Totally conscious/deliberate thing since it takes no effort to find where I live at all (I even do an open invite to to my house for holiday dinners).
Absolutely spot-on advice for those who don’t threat model and risk assess, tho.
Thank you for a helpful package, it looks great. However, when running it on Windows 7, I keep getting the error:
SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3GETSERVER_CERTIFICATE:certificate verify failed
I have tried adding ssl.verifypeer = FALSE to the function but still no luck. Do you know of any solution?
Hey @kheiga. I made some changes to the package that shld work (and tested it on a fresh Windows install). You may need to set the TZ variable ( ref: http://r.789695.n4.nabble.com/difftime-td875979.html ) since it doesn’t seem to be set by default on Windows (I primarily use OS X/Linux).
Awesome, it’s all working now. I am currently trying to find out how to change the function to retrieve the data in ‘si’ units, as the API offers ‘units’ as an option. In the mean time, manual conversion serves me just fine. Thank you again.
This looks great. I am trying to use this. Unfortunately, I can’t seem to fully grasp how the API wrapper works.
I have downloaded everything from the github site onto a desktop folder. I opened up the Rforecastio.R file and entered my API under the env <-Sys.getenv(‘abc#####’) and then tried to run fio.api.key = readLines(“~/.forecast.io”). I have the latest version of R and R tools and loaded all the libraries. the error I am seeing is:
I tried a work-around by creating a *.txt file with my API as the only entry. I then used read.table (“API.txt”). I could see my API appear in the console, but using it as a variable in the fio.api.key formula didn’t seem to work.
I also tried running > fio.api.key = readLines(“C:/Users/MyName/Desktop/MyWorkingDirectory/.forecast.io”) but had the same error. Even entering > fio.api.key = abc##### didn’t seem to work.
I’m sure I am making a basic mistake, I’m just not sure exactly where to enter my API. Any help would be greatly appreciated.
Okay, I spent a couple more hours. I now seem to be able to enter the API, but I’m running into issues with the command GET that appears in the Rforecastio.R script. I’ve loaded every library multiple times.
I tried running the ?forecastioapikey to determine the problem and recieved the following errors:
Error in fetch(key) :
lazy-load database ‘C:/Users/lindsayd/Documents/R/win-library/3.2/Rforecastio/help/Rforecastio.rdb’ is corrupt
In addition: Warning message:
In fetch(key) : internal error -3 in R_decompress1
Obviously, something is wrong, I just don’t know what it is. Any help is appreciated. As of now, I still cannot run the example shown above.
Thanks for trying out the pkg! It appears you have a corrupted install of Rforecastio. I’d suggest doing a devtools::install_github(“hrbrmstr/Rforecastio”) then follow the example on https://github.com/hrbrmstr/Rforecastio
Thank you for your reply. I really appreciate it. I uninstalled everything and re-installed R and Rtools. I tried running it again and received the following error:
Sorry, I had a typo. Please disregard my previous post, and thank you for your patience.
Cheers,
Damon
Most likely due to you spelling the package name wrong.
Hello ! I’m sorry but i don’t know where can i find the fio.json file. Thanks