Skip to content

Commit

Permalink
Adding progress bars to tract level pulls, fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1beb committed Feb 14, 2024
1 parent 08ada3e commit 9d8626a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
4 changes: 2 additions & 2 deletions R/census_geo_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ census_geo_api <- function(
message(paste("County ", county, " of ", length(county_list), ": ", county_list[county], sep = ""))
region_county <- paste("for=tract:*&in=state:", state.fips, "+county:", county_list[county], sep = "")
get_census_api(data_url = census_data_url, key = key, var.names = unlist(vars), region = region_county, retry)
})
}, .progress = TRUE)
} else {
message('There were no intersecting counties in your voter.file data (tract)')
}
Expand Down Expand Up @@ -192,7 +192,7 @@ census_geo_api <- function(

region_block <- paste("for=block:*&in=state:", state.fips, "+county:", county_list[county], "+tract:", tract_list[tract], sep = "")
get_census_api(census_data_url, key = key, var.names = unlist(vars), region = region_block, retry)
})
}, .progress = TRUE)
}
)
message("\n") # new line for progress bar
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-census_helper_v2.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ test_that("Fails if 'precinct' is set as the geo var",{
skip_on_cran()
set.seed(42)
data(voters)
future::plan(future::multisession)
census <- readRDS(test_path("data/new_census_table_NJ_2020.rds"))
expect_error(
census_helper_new(
Expand All @@ -32,6 +33,7 @@ test_that("helper returns verified census tract data",{
skip_on_cran()
set.seed(42)
data(voters)
future::plan(future::multisession)
census <- readRDS(test_path("data/new_census_table_NJ_2020.rds"))
x <- census_helper_new(
voter.file = voters,
Expand All @@ -56,6 +58,7 @@ test_that("New tables and legacy tables return equal race predictions",{
skip_on_cran()
set.seed(42)
data(voters)
future::plan(future::multisession)
# legacy redistricting table
census <- readRDS(test_path("data/census_test_nj_block_2020.rds"))
x <- census_helper_new(
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-get_census_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ if(Sys.getenv("CENSUS_API_KEY") != "") {
r <- suppressMessages(get_census_data(
key = NULL,
state = c("DC"),
census.geo = "block",
census.geo = "block"
))

expect_named(r$DC, c("state", "age", "sex", "year", "block", "tract", "county"))
expect_true(all(r$DC$block$state == "DC"))
})

test_that("Census block_group download works", {
future::plan(future::multisession)
r <- suppressMessages(get_census_data(
key = NULL,
state = "RI",
Expand All @@ -23,6 +24,7 @@ if(Sys.getenv("CENSUS_API_KEY") != "") {
})

test_that("Census tract download works", {
future::plan(future::multisession)
r <- suppressMessages(get_census_data(
key = NULL,
state = c("NY"),
Expand All @@ -35,6 +37,7 @@ if(Sys.getenv("CENSUS_API_KEY") != "") {
})

test_that("Census county download works", {
future::plan(future::multisession)
r <- suppressMessages(get_census_data(
key = NULL,
state = "NJ",
Expand All @@ -46,6 +49,7 @@ if(Sys.getenv("CENSUS_API_KEY") != "") {
})

test_that("Census place download works", {
future::plan(future::multisession)
r <- suppressMessages(get_census_data(
key = NULL,
state = "RI",
Expand Down
30 changes: 15 additions & 15 deletions tests/testthat/test-rollup.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ if(Sys.getenv("CENSUS_API_KEY") != "") {
))

r_tract_from_block <- r$DE$block |>
group_by(tract) |>
summarize(
dplyr::group_by(tract) |>
dplyr::summarize(
sum_whi = sum(r_whi),
sum_bla = sum(r_bla),
sum_his = sum(r_his),
Expand All @@ -19,9 +19,9 @@ if(Sys.getenv("CENSUS_API_KEY") != "") {
)

r_tract_level <- r$DE$tract |>
select(tract, r_whi:r_oth) |>
group_by(tract) |>
summarize(
dplyr::select(tract, r_whi:r_oth) |>
dplyr::group_by(tract) |>
dplyr::summarize(
sum_whi = sum(r_whi),
sum_bla = sum(r_bla),
sum_his = sum(r_his),
Expand All @@ -30,9 +30,9 @@ if(Sys.getenv("CENSUS_API_KEY") != "") {
)

r_county_from_tract <- r$DE$tract |>
select(county, r_whi:r_oth) |>
group_by(county) |>
summarize(
dplyr::select(county, r_whi:r_oth) |>
dplyr::group_by(county) |>
dplyr::summarize(
sum_whi = sum(r_whi),
sum_bla = sum(r_bla),
sum_his = sum(r_his),
Expand All @@ -41,9 +41,9 @@ if(Sys.getenv("CENSUS_API_KEY") != "") {
)

r_county_level <- r$DE$county |>
select(county, r_whi:r_oth) |>
group_by(county) |>
summarize(
dplyr::select(county, r_whi:r_oth) |>
dplyr::group_by(county) |>
dplyr::summarize(
sum_whi = sum(r_whi),
sum_bla = sum(r_bla),
sum_his = sum(r_his),
Expand Down Expand Up @@ -138,15 +138,15 @@ if(Sys.getenv("CENSUS_API_KEY") != "") {
))

r_sum_from_block <- r$AK$block |>
select(r_whi:r_fem_23_oth) |>
dplyr::select(r_whi:r_fem_23_oth) |>
apply(2, sum)

r_sum_from_tract <- r$AK$tract |>
select(r_whi:r_fem_23_oth) |>
dplyr::select(r_whi:r_fem_23_oth) |>
apply(2, sum)

r_sum_from_county <- r$AK$county |>
select(r_whi:r_fem_23_oth) |>
dplyr::select(r_whi:r_fem_23_oth) |>
apply(2, sum)

r_zcta_level <- suppressMessages(get_census_data(
Expand All @@ -158,7 +158,7 @@ if(Sys.getenv("CENSUS_API_KEY") != "") {
))

r_sum_from_zcta <- r_zcta_level$AK$zcta |>
select(r_whi:r_fem_23_oth) |>
dplyr::select(r_whi:r_fem_23_oth) |>
apply(2, sum)

expect_true(
Expand Down

0 comments on commit 9d8626a

Please sign in to comment.