Rforecastio – Simple R Package To Access forecast.io Weather Data

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.

install_github("Rforecastio", "hrbrmstr")
# 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()


Cover image from Data-Driven Security
Amazon Author Page

15 Comments Rforecastio – Simple R Package To Access forecast.io Weather Data

    1. hrbrmstr

      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.

    1. hrbrmstr


      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.

  1. khelga

    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?

  2. khelga

    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.

  3. Damon W

    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:

    fio.api.key = readLines(“~/.forecast.io”)
    Error in file(con, “r”) : cannot open the connection
    In addition: Warning message:
    In file(con, “r”) :
    cannot open file ‘C:/Users/MyName/Documents/.forecast.io’: No such file or directory

    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.

    1. Damon W

      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.

      Couldn’t find env var FORECASTIOAPIKEY See ?forecastioapikey for more details.
      Please enter your API key and press enter:
      : c66b7ceac7065a45d############
      Updating FORECASTIOAPIKEY env var to PAT
      Error in getcurrentforecast(43.61, -116.1988, “us”, “en”, NULL, NULL) :
      could not find function “GET”

      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


      Warning messages:
      1: In fetch(key) : internal error -3 in Rdecompress1
      2: In fetch(key) : internal error -3 in R

      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.

        1. Damon W

          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:

          Downloading GitHub repo hrbrmstr/Rforcastio@master
          Error in download(dest, src, auth) : client error: (404) Not Found

          1. Damon W

            Sorry, I had a typo. Please disregard my previous post, and thank you for your patience.


  4. StatisticStudent

    Hello ! I’m sorry but i don’t know where can i find the fio.json file. Thanks


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.