-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 186ab45
Showing
9 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)")) | ||
} | ||
} |