Skip to content

Commit

Permalink
updates to support a workflow via shiny
Browse files Browse the repository at this point in the history
  • Loading branch information
alamstein-trulia committed Mar 9, 2014
1 parent bd5ce07 commit 1b197c3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export(bind_df_to_map)
export(choroplethr)
export(choroplethr_acs)
export(get_acs_column_names)
export(get_acs_df)
export(render_choropleth)
importFrom(Hmisc,cut2)
Expand Down
50 changes: 36 additions & 14 deletions R/choroplethr_acs.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,25 @@ choroplethr_acs = function(tableId, lod, num_buckets = 9, showLabels = T, states
#' @param lod The level of geographic detail for the data.frame. Must be one of "state", "county" or "zip".
#' @param endyear The end year of the survey. Defaults to 2012.
#' @param span The span of the survey. Defaults to 5.
#' @param column_idx An optional column index to specify.
#' @return A data.frame
#' @export
#' @seealso http://factfinder2.census.gov/faces/help/jsf/pages/metadata.xhtml?lang=en&type=survey&id=survey.en.ACS_ACS, which lists all ACS Surveys.
#' @importFrom acs acs.fetch geography estimate geo.make
get_acs_df = function(tableId, lod, endyear=2012, span=5)
get_acs_df = function(tableId, lod, endyear=2012, span=5, column_idx = -1)
{
stopifnot(lod %in% c("state", "county", "zip"))

acs.data = acs.fetch(geography=make_geo(lod), table.number = tableId, col.names = "pretty", endyear = endyear, span = span)
column_idx = get_column_idx(acs.data, tableId) # some tables have multiple columns
if (column_idx == -1) {
column_idx = get_column_idx(acs.data, tableId) # some tables have multiple columns
}
acs.df = make_df(lod, acs.data, column_idx) # turn into df
acs.df$region = as.character(acs.df$region)

acs.df
}

make_geo = function(lod)
{
stopifnot(lod %in% c("state", "county", "zip"))
if (lod == "state") {
geo.make(state = "*")
} else if (lod == "county") {
geo.make(state = "*", county = "*")
} else {
geo.make(zip.code = "*")
}
}

# support multiple column tables
get_column_idx = function(acs.data, tableId)
{
Expand All @@ -99,6 +90,37 @@ get_column_idx = function(acs.data, tableId)
column_idx
}

#' Returns the column names of an ACS table.
#'
#' @param tableId The id of an ACS table.
#' @param lod The level of geographic detail for the data.frame. Must be one of "state", "county" or "zip".
#' @param endyear The end year of the survey. Defaults to 2012.
#' @param span The span of the survey. Defaults to 5.
#' @return A vector of column names
#' @export
#' @seealso http://factfinder2.census.gov/faces/help/jsf/pages/metadata.xhtml?lang=en&type=survey&id=survey.en.ACS_ACS, which lists all ACS Surveys.
#' @importFrom acs acs.fetch geography estimate geo.make
get_acs_column_names = function(tableId, lod, endyear=2012, span=5)
{
stopifnot(lod %in% c("state", "county", "zip"))
stopifnot(span %in% c(1, 3, 5))

acs.data = acs.fetch(geography=make_geo(lod), table.number = tableId, col.names = "pretty", endyear = endyear, span = span)
acs.data@acs.colnames
}

make_geo = function(lod)
{
stopifnot(lod %in% c("state", "county", "zip"))
if (lod == "state") {
geo.make(state = "*")
} else if (lod == "county") {
geo.make(state = "*", county = "*")
} else {
geo.make(zip.code = "*")
}
}

make_df = function(lod, acs.data, column_idx)
{
stopifnot(lod %in% c("state", "county", "zip"))
Expand Down
28 changes: 28 additions & 0 deletions man/get_acs_column_names.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
\name{get_acs_column_names}
\alias{get_acs_column_names}
\title{Returns the column names of an ACS table.}
\usage{
get_acs_column_names(tableId, lod, endyear = 2012, span = 5)
}
\arguments{
\item{tableId}{The id of an ACS table.}

\item{lod}{The level of geographic detail for the
data.frame. Must be one of "state", "county" or "zip".}

\item{endyear}{The end year of the survey. Defaults to
2012.}

\item{span}{The span of the survey. Defaults to 5.}
}
\value{
A vector of column names
}
\description{
Returns the column names of an ACS table.
}
\seealso{
http://factfinder2.census.gov/faces/help/jsf/pages/metadata.xhtml?lang=en&type=survey&id=survey.en.ACS_ACS,
which lists all ACS Surveys.
}

4 changes: 3 additions & 1 deletion man/get_acs_df.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
\alias{get_acs_df}
\title{Returns a data.frame representing American Community Survey estimates.}
\usage{
get_acs_df(tableId, lod, endyear = 2012, span = 5)
get_acs_df(tableId, lod, endyear = 2012, span = 5, column_idx = -1)
}
\arguments{
\item{tableId}{The id of an ACS table.}
Expand All @@ -14,6 +14,8 @@ get_acs_df(tableId, lod, endyear = 2012, span = 5)
2012.}

\item{span}{The span of the survey. Defaults to 5.}

\item{column_idx}{An optional column index to specify.}
}
\value{
A data.frame
Expand Down

0 comments on commit 1b197c3

Please sign in to comment.