Thanks to a comment suggestion, the Rforecastio package is now up to version 1.3.0 and has a new parameter which lets you specify which time conversion function you want to use. Details are up on [github](https://github.com/hrbrmstr/Rforecastio).
Tag Archives: Rforecastio
Not even going to put an `R` category on this since I don’t want to pollute R-bloggers with this tiny post, but I had to provide the option to let folks specify `ssl.verifypeer=FALSE` (so I made it a generic option to pass in any CURL parameters) and I had a couple gaping bugs that I missed due to not clearing out my environment before building & testing.
I’ve bumped up the version number of `Rforecastio` ([github](https://github.com/hrbrmstr/Rforecastio)) to `1.1.0`. The new
features are:
– removing the SSL certificate bypass check (it doesn’t need it
anymore)
– using `plyr` for easier conversion of JSON-\>data frame
– adding in a new `daily` forecast data frame
– roxygen2 inline documentation
library(Rforecastio) library(ggplot2) library(plyr) # NEVER put API keys in revision control systems or source code! 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) fio.gg <- ggplot(data=fio.list$hourly.df, aes(x=time, y=temperature)) fio.gg <- fio.gg + labs(y="Readings", x="Time", title="Houry Readings") 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
fio.gg <- ggplot(data=fio.list$daily.df, aes(x=time, y=temperature)) fio.gg <- fio.gg + labs(y="Readings", x="Time", title="Daily Readings") fio.gg <- fio.gg + geom_line(aes(y=humidity*100), color="green") fio.gg <- fio.gg + geom_line(aes(y=temperatureMax), color="red") fio.gg <- fio.gg + geom_line(aes(y=temperatureMin), color="red", linetype=2) fio.gg <- fio.gg + geom_line(aes(y=dewPoint), color="blue") fio.gg <- fio.gg + theme_bw() fio.gg
Thanks to a comment, I tweaked the data retrieval to ignore SSL cert errors. You can change that tweak back if you go through the pain of updating the SSL libraries on your Windows boxes (it doesn’t seem to be an issue on OS X/Linux).
I also changed the date routines to use as.POSIXlt
instead of ISOdatetime
as the latter seemed to cause issues for some folks.
All changes have been pushed to the github repo.
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