Skip to content

Commit

Permalink
Merge pull request #36 from sysilviakim/master
Browse files Browse the repository at this point in the history
Saving/checking/loading a temporary file in census_geo_api (#32 continued)
  • Loading branch information
kosukeimai authored Jan 13, 2021
2 parents b07e8a1 + e2b1f03 commit 54f1507
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ notifications:
email:
on_success: change
on_failure: change

addons:
apt:
packages:
- libgit2-dev
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ Suggests:
LazyLoad: yes
LazyData: yes
License: GPL (>= 3)
RoxygenNote: 6.0.1
RoxygenNote: 7.1.1
38 changes: 35 additions & 3 deletions R/census_geo_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#' If \code{TRUE}, function will return Pr(Geolocation, Sex | Race).
#' If \code{\var{age}} is also \code{TRUE}, function will return Pr(Geolocation, Age, Sex | Race).
#' @param retry The number of retries at the census website if network interruption occurs.
#' @param save_temp File indicating where to save the temporary outputs.
#' Defaults to NULL. If specified, the function will look for an .RData file
#' with the same format as the expected output.
#' @return Output will be an object of class \code{list}, indexed by state names. It will
#' consist of the original user-input data with additional columns of Census geographic data.
#'
Expand All @@ -34,7 +37,7 @@
#' available \href{https://rstudio-pubs-static.s3.amazonaws.com/19337_2e7f827190514c569ea136db788ce850.html}{here}.
#'
#' @export
census_geo_api <- function(key, state, geo = "tract", age = FALSE, sex = FALSE, retry = 0) {
census_geo_api <- function(key, state, geo = "tract", age = FALSE, sex = FALSE, retry = 0, save_temp = NULL) {

if (missing(key)) {
stop('Must enter U.S. Census API key, which can be requested at https://api.census.gov/data/key_signup.html.')
Expand Down Expand Up @@ -99,13 +102,19 @@ census_geo_api <- function(key, state, geo = "tract", age = FALSE, sex = FALSE,
region_county <- paste("for=county:*&in=state:", state.fips, sep = "")
county_df <- get_census_api("https://api.census.gov/data/2010/dec/sf1?", key = key, vars = vars, region = region_county, retry)
county_list <- county_df$county

census <- NULL
temp <- check_temp_save(county_list, save_temp, census)
county_list <- temp$county_list
census <- temp$census

for (c in 1:length(county_list)) {
print(paste("County ", c, " of ", length(county_list), ": ", county_list[c], sep = ""))
region_county <- paste("for=tract:*&in=state:", state.fips, "+county:", county_list[c], sep = "")
census.temp <- get_census_api("https://api.census.gov/data/2010/dec/sf1?", key = key, vars = vars, region = region_county, retry)
census <- rbind(census, census.temp)
if (!is.null(save_temp)) {
save(census, file = save_temp)
}
}
rm(census.temp)
}
Expand All @@ -117,8 +126,10 @@ census_geo_api <- function(key, state, geo = "tract", age = FALSE, sex = FALSE,
region_county <- paste("for=county:*&in=state:", state.fips, sep = "")
county_df <- get_census_api("https://api.census.gov/data/2010/dec/sf1?", key = key, vars = vars, region = region_county, retry)
county_list <- county_df$county

census <- NULL
temp <- check_temp_save(county_list, save_temp, census)
county_list <- temp$county_list
census <- temp$census

for (c in 1:length(county_list)) {
print(paste("County ", c, " of ", length(county_list), ": ", county_list[c], sep = ""))
Expand All @@ -135,6 +146,9 @@ census_geo_api <- function(key, state, geo = "tract", age = FALSE, sex = FALSE,
census.temp <- get_census_api("https://api.census.gov/data/2010/dec/sf1?", key = key, vars = vars, region = region_block, retry)
census <- rbind(census, census.temp)
}
if (!is.null(save_temp)) {
save(census, file = save_temp)
}
}

rm(census.temp)
Expand Down Expand Up @@ -236,3 +250,21 @@ census_geo_api <- function(key, state, geo = "tract", age = FALSE, sex = FALSE,

return(census)
}

check_temp_save <- function(county_list, save_temp, census) {
if (!is.null(save_temp)) {
if (file.exists(save_temp)) {
message("Temporary save file will be used as requested.")
load(save_temp)
## Expecting a dataframe named census with the same format
county_list <- setdiff(county_list, unique(census$county))
message(paste0(
length(unique(census$county)), " counties in the temporary file."
))
message(paste0(length(county_list), " counties to be processed."))
} else {
message("Results will be saved in the specified temporary file.")
}
}
return(list(county_list = county_list, census = census))
}
15 changes: 13 additions & 2 deletions man/census_geo_api.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions man/census_helper.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/get_census_api.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/get_census_api_2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions man/get_census_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions man/merge_surnames.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions man/predict_race.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/surnames2000.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/surnames2010.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/voters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/testthat/test-all.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ test_that("Tests predictions using the Census object", {

# Build a Census object by parts; both county-level and tract-level statistics needed for tract-level predictions
censusObj2 <- list()
county.dc <- census_geo_api(key = k, state = "DC", geo = "county", age = TRUE, sex = FALSE)
tract.dc <- census_geo_api(key = k, state = "DC", geo = "tract", age = TRUE, sex = FALSE)
county.dc <- census_geo_api(key = k, state = "DC", geo = "county", age = TRUE, sex = FALSE, save_temp = NULL)
tract.dc <- census_geo_api(key = k, state = "DC", geo = "tract", age = TRUE, sex = FALSE, save_temp = NULL)
censusObj2[["DC"]] <- list(state = "DC", county = county.dc, tract = tract.dc, age = TRUE, sex = FALSE)
tract.nj <- census_geo_api(key = k, state = "NJ", geo = "tract", age = TRUE, sex = FALSE)
county.nj <- census_geo_api(key = k, state = "NJ", geo = "county", age = TRUE, sex = FALSE)
tract.nj <- census_geo_api(key = k, state = "NJ", geo = "tract", age = TRUE, sex = FALSE, save_temp = NULL)
county.nj <- census_geo_api(key = k, state = "NJ", geo = "county", age = TRUE, sex = FALSE, save_temp = NULL)
censusObj2[["NJ"]] <- list(state = "NJ", county = county.nj, tract = tract.nj, age = TRUE, sex = FALSE)

# Prediction using the Census object built in the previous step; county-level statistics used in prediction
Expand Down

0 comments on commit 54f1507

Please sign in to comment.