Skip to content

Commit

Permalink
Initial Setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-Brigida committed Aug 7, 2014
0 parents commit 186ab45
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.Rproj.user
.Rhistory
.RData
10 changes: 10 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Package: EIAdata
Type: Package
Title: R Wrapper for the Energy Information Administration (EIA) API
Version: 0.0.1
Date: 2014-08-04
Author: Matthew Brigida
Maintainer: Matthew Brigida <matt@complete-markets.com>
Description: An R wrapper to allow the user to query categories and Series IDs, and import data, from the EIA's API.
Depends: R (>= 2.11.0), XML, plyr, xts, zoo
License: GPL-2
17 changes: 17 additions & 0 deletions EIAdata.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
importFrom(XML, xmlParse)
importFrom(XML, xmlToDataFrame)
importFrom(plyr, arrange)
importFrom(xts, xts)
importFrom(zoo, as.yearqtr)

export(getCatEIA)
export(getEIA)
130 changes: 130 additions & 0 deletions R/source.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@

.last_char <- function(x){
substr(x, nchar(x), nchar(x))
}

getEIA <- function(ID, key){

switch(.last_char(ID),
"A" = .getAnnEIA(ID, key=key),
"Q" = .getQEIA(ID, key=key),
"M" = .getMonEIA(ID, key=key),
"W" = .getWDEIA(ID, key=key),
"D" = .getWDEIA(ID, key=key),
print("ERROR: The last character of your ID is not one of the possible sampling frequencies (A, Q, M, W, or D)"))
}


.getAnnEIA <- function(ID, key){

ID <- unlist(strsplit(ID, ";"))
key <- unlist(strsplit(key, ";"))

url <- paste("http://api.eia.gov/series?series_id=", ID, "&api_key=", key, "&out=xml", sep="" )

doc <- xmlParse(file=url, isURL=TRUE)

df <- xmlToDataFrame(nodes = getNodeSet(doc, "//data/row"))

df <- arrange(df, df$date)
date <- as.Date(paste(as.character(levels(df[,1]))[df[,1]], "-12-31", sep=""), "%Y-%m-%d")
values <- as.numeric(levels(df[,-1]))[df[,-1]]

xts_data <- xts(values, order.by=date)
names(xts_data) <- sapply(strsplit(ID, "-"), paste, collapse = ".")

temp <- assign(sapply(strsplit(ID, "-"), paste, collapse = "."), xts_data)
return(temp)
}

.getQEIA <- function(ID, key){

ID <- unlist(strsplit(ID, ";"))
key <- unlist(strsplit(key, ";"))

url <- paste("http://api.eia.gov/series?series_id=", ID, "&api_key=", key, "&out=xml", sep="" )

doc <- xmlParse(file=url, isURL=TRUE)

df <- xmlToDataFrame(nodes = getNodeSet(doc, "//data/row"))

df <- arrange(df, df$date)

date <- as.yearqtr(df$date)
values <- as.numeric(levels(df[,-1]))[df[,-1]]

xts_data <- xts(values, order.by=date)
names(xts_data) <- sapply(strsplit(ID, "-"), paste, collapse = ".")

temp <- assign(sapply(strsplit(ID, "-"), paste, collapse = "."), xts_data)
return(temp)
}

.getMonEIA <- function(ID, key){

ID <- unlist(strsplit(ID, ";"))
key <- unlist(strsplit(key, ";"))

url <- paste("http://api.eia.gov/series?series_id=", ID, "&api_key=", key, "&out=xml", sep="" )

doc <- xmlParse(file=url, isURL=TRUE)

df <- xmlToDataFrame(nodes = getNodeSet(doc, "//data/row"))

df <- arrange(df, df$date)

date <- as.Date(paste(as.character(levels(df[,1]))[df[,1]], "01", sep=""), "%Y%m%d")
values <- as.numeric(levels(df[,-1]))[df[,-1]]

xts_data <- xts(values, order.by=date)
names(xts_data) <- sapply(strsplit(ID, "-"), paste, collapse = ".")

temp <- assign(sapply(strsplit(ID, "-"), paste, collapse = "."), xts_data)
return(temp)
}

.getWDEIA <- function(ID, key){

ID <- unlist(strsplit(ID, ";"))
key <- unlist(strsplit(key, ";"))

url <- paste("http://api.eia.gov/series?series_id=", ID, "&api_key=", key, "&out=xml", sep="" )

doc <- xmlParse(file=url, isURL=TRUE)

df <- xmlToDataFrame(nodes = getNodeSet(doc, "//data/row"))

df <- arrange(df, df$date)

date <- as.Date(df$date, "%Y%m%d")
values <- as.numeric(levels(df[,-1]))[df[,-1]]

xts_data <- xts(values, order.by=date)
names(xts_data) <- sapply(strsplit(ID, "-"), paste, collapse = ".")

temp <- assign(sapply(strsplit(ID, "-"), paste, collapse = "."), xts_data)
return(temp)
}

getCatEIA <- function(key, cat=999999999){

key <- unlist(strsplit(key, ";"))

ifelse(cat==999999999,
url <- paste("http://api.eia.gov/category?api_key=", key, "&out=xml", sep="" ),

url <- paste("http://api.eia.gov/category?api_key=", key, "&category_id=", cat, "&out=xml", sep="" )
)

doc <- xmlParse(file=url, isURL=TRUE)

print("########Parent Category########")
tryCatch(print(xmlToDataFrame(nodes = getNodeSet(doc, "//category/parent_category_id"))), warning=function(w) FALSE, error=function(w) FALSE)

print("########Sub-Categories########")
print(xmlToDataFrame(nodes = getNodeSet(doc, "//childcategories/row")))


print("########Series IDs########")
print(xmlToDataFrame(nodes = getNodeSet(doc, "///childseries/row")))
}
54 changes: 54 additions & 0 deletions man/EIAdata-package.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
\name{EIAdata-package}
\alias{EIAdata-package}
\alias{EIAdata}
\docType{package}
\title{
R wrapper for the US Energy Information Administration's (EIA's) API.
}
\description{
This package allows the user to query categories, and import data, through the EIA's API.
Resulting time series are objects of class xts.
}
\details{
\tabular{ll}{
Package: \tab EIAdata\cr
Type: \tab Package\cr
Version: \tab 0.0.1\cr
Date: \tab 2014-08-04\cr
License: \tab GPL-2\cr
}
}
\author{
Matthew Brigida
Maintainer: Matthew Brigida <matt@complete-markets.com>
}
\references{
http://www.eia.gov/beta/api/
}
\keyword{ EIAdata }
\examples{
\donttest{
# Be sure to load your EIA key. EIA keys are free. You can request one
# here: http://www.eia.gov/beta/api/register.cfm
key <- "your_key"
# To see the top of the data category hierarchy.
getCatEIA(key=key)
# To see the subcategories and data sets in a particular category (for example 40827).
getCatEIA(key=key, cat=40827)
# To download and return a time series object of class xts
# for example ELEC.PLANT.GEN.13-WAT-ALL.Q
getQEIA(ID = "ELEC.PLANT.GEN.13-WAT-ALL.Q", key = key)
# The if the EIA series ID contains a "-", the function will replace
# this with a ".". So the call above will return a time series of
# class xts named ELEC.PLANT.GEN.13.WAT.ALL.Q
}
}
38 changes: 38 additions & 0 deletions man/getCatEIA.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
\name{getCatEIA}
\alias{getCatEIA}
\title{A function to view the sub and parent categories of an Energy
Information Administration (EIA) API data category.
}
\description{A function to view the sub and parent categories of a EIA API
data category. The function will return Series IDs in a category if present.
}
\usage{
getCatEIA(key, cat = 999999999)
}
\arguments{
\item{key}{Your EIA API key, in quotes.
}
\item{cat}{An EIA API data category number.
}
}
\author{Matthew Brigida
}
\examples{

## The function is currently defined as
function (key, cat = 999999999)
{
key <- unlist(strsplit(key, ";"))
ifelse(cat == 999999999, url <- paste("http://api.eia.gov/category?api_key=",
key, "&out=xml", sep = ""), url <- paste("http://api.eia.gov/category?api_key=",
key, "&category_id=", cat, "&out=xml", sep = ""))
doc <- xmlParse(file = url, isURL = TRUE)
print("########Parent Category########")
tryCatch(print(xmlToDataFrame(nodes = getNodeSet(doc, "//category/parent_category_id"))),
warning = function(w) FALSE, error = function(w) FALSE)
print("########Sub-Categories########")
print(xmlToDataFrame(nodes = getNodeSet(doc, "//childcategories/row")))
print("########Series IDs########")
print(xmlToDataFrame(nodes = getNodeSet(doc, "///childseries/row")))
}
}
31 changes: 31 additions & 0 deletions man/getEIA.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
\name{getEIA}
\alias{getEIA}
\title{A function to download data from the Energy Information Administration's (EIA's) API.
}
\description{A function to download data from the EIA's API. Resulting time series are of class xts.
}
\usage{
getEIA(ID, key)
}
\arguments{
\item{ID}{The EIA API Series ID for the data.
}
\item{key}{Your EIA API key.
}
}
\value{xts object (time series)
}
\author{Matthew Brigida
}
\examples{
## The function is currently defined as
function (ID, key)
{
switch(.last_char(ID), A = .getAnnEIA(ID, key = key), Q = .getQEIA(ID,
key = key), M = .getMonEIA(ID, key = key), W = .getWDEIA(ID,
key = key), D = .getWDEIA(ID, key = key),
print("ERROR: The last
character of your ID is not one of the possible sampling
frequencies (A, Q, M, W, or D)"))
}
}

0 comments on commit 186ab45

Please sign in to comment.