From 62f768d3f20dc3a2ad314bab8bb1e3cd0698000c Mon Sep 17 00:00:00 2001 From: Hannah Recht Date: Sun, 19 Aug 2018 16:14:35 -0400 Subject: [PATCH] add group option to listCensusMetadata, re #45 --- NEWS.md | 1 + R/metadata_functions.R | 31 +++++++++++++------- docs/articles/example-masterlist.html | 24 ++++++++-------- docs/news/index.html | 1 + docs/reference/listCensusApis.html | 40 +++++++++++++------------- docs/reference/listCensusMetadata.html | 40 ++++++++++++++++++++------ man/listCensusMetadata.Rd | 19 ++++++------ 7 files changed, 97 insertions(+), 59 deletions(-) diff --git a/NEWS.md b/NEWS.md index 88809e6..90268d3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,5 @@ # censusapi 0.4.1 +* Adds `groups` type option to `listCensusMetadata`. * Fixes bug in `listCensusMetadata` variables call caused by an underlying Census API change, which renamed `validValues` to `values` for some endpoints. * Converts variable metadata columns from factors to characters. * Applies numeric conversion exclusions to all API endpoints. diff --git a/R/metadata_functions.R b/R/metadata_functions.R index b72ba85..463e7e8 100644 --- a/R/metadata_functions.R +++ b/R/metadata_functions.R @@ -29,19 +29,22 @@ listCensusApis <- function() { #' #' @param name API name - e.g. acs5. See list at https://api.census.gov/data.html #' @param vintage Vintage of dataset, e.g. 2014 - not required for timeseries APIs -#' @param type Type of metadata to return, either "variables" or "v" to return variables -#' or "geographies" or "g" to return geographies. Default is variables. +#' @param type Type of metadata to return, either "variables", "geographies" or "geography", or +#' "groups". Default is variables. #' @keywords metadata #' @export #' @examples -#' varsbds<- listCensusMetadata(name = "timeseries/bds/firms", type = "variables") -#' head(varsbds) +#' bds_vars <- listCensusMetadata(name = "timeseries/bds/firms", type = "variables") +#' head(bds_vars) #' -#' geosbds <- listCensusMetadata(name = "timeseries/bds/firms", type = "geography") -#' head(geosbds) +#' bds_geos <- listCensusMetadata(name = "timeseries/bds/firms", type = "geography") +#' head(bds_geos) #' -#' geosacs <- listCensusMetadata(name = "acs/acs5", vintage = 2016, type = "geography") -#' head(geosacs) +#' acs_geos <- listCensusMetadata(name = "acs/acs5", vintage = 2016, type = "geography") +#' head(acs_geos) +#' +#' acs_groups <- listCensusMetadata(name = "acs/acs5", vintage = 2016, type = "groups") +#' head(acs_groups) listCensusMetadata <- function(name, vintage=NULL, type="variables") { constructURL <- function(name, vintage) { if (is.null(vintage)) { @@ -95,8 +98,16 @@ listCensusMetadata <- function(name, vintage=NULL, type="variables") { # Simple json to data.frame conversion raw <- jsonlite::fromJSON(u) dt <- raw$fips - } else { - stop(paste('For "type", you entered: "', type, '". Did you mean "variables" or "geography"?', sep = "")) + } else if (type %in% c("groups", "group")) { + u <- paste(apiurl, "groups.json", sep="/") + # Simple json to data.frame conversion + raw <- jsonlite::fromJSON(u) + dt <- raw[[1]] + if (is.null(dim(dt))) { + stop("Groups are not available for the selected API endpoint.") + } + } else { + stop(paste('For "type", you entered: "', type, '". Did you mean "variables" or "geography" or "groups"?', sep = "")) } return(dt) } diff --git a/docs/articles/example-masterlist.html b/docs/articles/example-masterlist.html index 842d133..3b4686c 100644 --- a/docs/articles/example-masterlist.html +++ b/docs/articles/example-masterlist.html @@ -1041,30 +1041,30 @@

54 -08 -Colorado -3709131 +04 +Arizona +2991782 Professional, scientific, and technical services 54 -10 -Delaware -600556 +05 +Arkansas +616936 Professional, scientific, and technical services 54 -12 -Florida -11147761 +06 +California +28746664 Professional, scientific, and technical services 54 -04 -Arizona -2991782 +08 +Colorado +3709131 Professional, scientific, and technical services 54 diff --git a/docs/news/index.html b/docs/news/index.html index 27d1d44..b359253 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -106,6 +106,7 @@

censusapi 0.4.1 Unreleased