Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating testthat + vignette .html and .Rmd #38

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
83 changes: 43 additions & 40 deletions tests/testthat/test-get_geo_estimates_function.R
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
test_that("missing parameters are correctly handled", {

### EXPECT MESSAGE
# default var_codes, no geo, no boundary_year
expect_message(get_geo_estimates(), "requires a 'geo' parameter", class = "simpleMessage")
# default var_codes, no geo, yes boundary_year
expect_message(get_geo_estimates(boundary_year = "2023"), "requires a 'geo' parameter", class = "simpleMessage")
# yes var_codes, no geo, no boundary_year,
expect_message(get_geo_estimates(var_codes = c("DP02_0113E","DP02_0115E")), "requires a 'geo' parameter", class = "simpleMessage")
# yes var_codes, no geo, yes boundary_year
expect_message(get_geo_estimates(var_codes = c("DP02_0113E","DP02_0115E"), boundary_year = "2023"), "requires a 'geo' parameter", class = "simpleMessage")
# geo and boundary_year provided without parameter names
expect_message(get_geo_estimates("councildist", "2023"), "boundary_year input used for var_codes parameter", class = "simpleMessage")
# no inputs
expect_message(get_geo_estimates(), "requires an `acs_year` parameter", class = "simpleMessage")
# no `acs_year`, default `var_codes`, yes `geo`, no `boundary_year`
expect_message(get_geo_estimates(geo="borough"), "requires an `acs_year` parameter", class = "simpleMessage")
# yes `acs_year`, default `var_codes`, no `geo`, no `boundary_year`
expect_message(get_geo_estimates(acs_year="2022"), "requires a `geo` parameter", class = "simpleMessage")
# no `acs_year`, default `var_codes`, no `geo`, yes `boundary_year`
expect_message(get_geo_estimates(boundary_year="2023"), "requires an `acs_year` parameter", class = "simpleMessage")
# no `acs_year`, yes `var_codes`, no `geo`, no `boundary_year`,
expect_message(get_geo_estimates(var_codes=c("DP02_0113E","DP02_0115E")), "requires an `acs_year` parameter", class = "simpleMessage")
# yes `acs_year`, yes `var_codes`, no `geo`, yes `boundary_year`
expect_message(get_geo_estimates(acs_year="2022", var_codes=c("DP02_0113E","DP02_0115E"), boundary_year="2023"), "requires a `geo` parameter", class = "simpleMessage")

})

test_that("incorrect geo and var_codes inputs are handled appropriately", {
test_that("incorrect inputs are handled appropriately", {

### EXPECT MESSAGE
# invalid geo
expect_message(get_geo_estimates("block party", c("DP02_0113E","DP02_0115E"), "2023"), "This geography could not be found", class = "simpleMessage")
# typo in var_codes
expect_message(get_geo_estimates("councildist", c("0000","DP02_0115E"), "2023"), "The following variable codes could not be found", class = "simpleMessage")
### EXPECT MESSAGE OR ERROR
# invalid `acs_year`
expect_message(get_geo_estimates("202", "borough", c("DP02_0113E","DP02_0115E"), "2023"), 'The ACS year "202" could not be found', class = "simpleMessage")
# invalid `geo`
expect_message(get_geo_estimates("2022", "block party", c("DP02_0113E","DP02_0115E"), "2023"), 'The geography "block party" could not be found', class = "simpleMessage")
# typo in `var_codes`
expect_error(get_geo_estimates("2022", "councildist", c("0000", "DP02_0115E"), "2023"), "Estimates for the variable code 0000 are not available", class = "simpleError")
# invalid `boundary_year`
expect_message(get_geo_estimates("2022", "councildist", c("DP02_0113E","DP02_0115E"), "2024"), "Overriding `boundary_year` input to 2023", class = "simpleMessage")
# inputs provided without parameter names + default `var_codes`
expect_error(get_geo_estimates("2022", "councildist", "2023"), "Estimates for the variable code 2023 are not available", class = "simpleError")

})

test_that("missing, unnecessary, and incorrect boundary_year inputs are handled appropriately", {
test_that("missing or unnecessary `boundary_year` inputs are handled appropriately", {

### EXPECT MESSAGE
# council w/ no year
expect_message(get_geo_estimates("councildist", c("DP02_0113E","DP02_0115E")), "Setting `boundary_year`", class = "simpleMessage")
# council w/ incorrect year
expect_message(get_geo_estimates("councildist", c("DP02_0113E","DP02_0115E"), "2053"), "`boundary_year` must be '2013' or '2023'", class = "simpleMessage")
# non-council with incorrect year
expect_message(get_geo_estimates("borough", c("DP02_0113E","DP02_0115E"), "2043"), "`boundary_year` is only relevant for `geo = councildist`", class = "simpleMessage")
# non-council with unnecessary year, yes var_codes
expect_message(get_geo_estimates("borough", c("DP02_0113E","DP02_0115E"), "2023"), "`boundary_year` is only relevant for `geo = councildist`", class = "simpleMessage")
# non-council with unnecessary year, default var_codes
expect_message(get_geo_estimates(geo = "borough", boundary_year = "2023"), "`boundary_year` is only relevant for `geo = councildist`", class = "simpleMessage")
# councildist w/ no year
expect_message(get_geo_estimates("2022", "councildist", c("DP02_0113E","DP02_0115E")), "Overriding `boundary_year` input to 2023", class = "simpleMessage")
# non-councildist with incorrect year
expect_message(get_geo_estimates("2022", "borough", c("DP02_0113E","DP02_0115E"), "2043"), "Overriding `boundary_year` input to NULL", class = "simpleMessage")
# non-councildist with unnecessary (but correct) `boundary_year`, yes `var_codes`
expect_message(get_geo_estimates("2022", "borough", c("DP02_0113E","DP02_0115E"), "2023"), "Overriding `boundary_year` input to NULL", class = "simpleMessage")
# non-councildist with unnecessary (but correct) `boundary_year`, default var_codes
expect_message(get_geo_estimates("2022", geo = "borough", boundary_year = "2023"), "Overriding `boundary_year` input to NULL", class = "simpleMessage")

})

test_that("correct inputs work", {

### EXPECT NO MESSAGE
# yes var_codes, council 2023
expect_no_message(get_geo_estimates("councildist", c("DP02_0113E","DP02_0115E"), "2023"), message = "`boundary_year` must be '2013' or '2023'", class = "simpleMessage")
# yes var_codes, council 2013
expect_no_message(get_geo_estimates("councildist", c("DP02_0113E","DP02_0115E"), "2013"), message = "`boundary_year` must be '2013' or '2023'", class = "simpleMessage")
# yes var_codes, non-council w/ no year
expect_no_message(get_geo_estimates("borough", c("DP02_0113E","DP02_0115E")), message = "`boundary_year` must be '2013' or '2023'", class = "simpleMessage")
# default var_codes, non-council w/ no year
expect_no_message(get_geo_estimates("borough"), message = "`boundary_year` must be '2013' or '2023'", class = "simpleMessage")
# "all" input for var_codes, non-council w/ no year
expect_no_message(get_geo_estimates("borough", "all"), message = "`boundary_year` must be '2013' or '2023'", class = "simpleMessage")
# geo and boundary_year provided with parameter names, default var_code
expect_no_message(get_geo_estimates(geo = "councildist", boundary_year = "2023"), message = "boundary_year input used for var_codes parameter", class = "simpleMessage")
# 'councildist' chosen, `boundary_year` included
expect_no_message(get_geo_estimates("2022" ,"councildist", c("DP02_0113E","DP02_0115E"), "2023"), message = "`boundary_year` must be '2013' or '2023'", class = "simpleMessage")
# non-councildist w/ no `boundary_year`
expect_no_message(get_geo_estimates("2022", "borough", c("DP02_0113E","DP02_0115E")), message = "`boundary_year` must be '2013' or '2023'", class = "simpleMessage")
# default `var_codes`, non-councildist w/ no `boundary_year`
expect_no_message(get_geo_estimates("2022", "borough"), message = "`boundary_year` must be '2013' or '2023'", class = "simpleMessage")
# "all" input for `var_codes`, non-councildist w/ no year
expect_no_message(get_geo_estimates("2022", "borough", "all"), message = "`boundary_year` must be '2013' or '2023'", class = "simpleMessage")
# `acs_year`, `geo`, and `boundary_year` provided with parameter names, default `var_codes`
expect_no_message(get_geo_estimates(acs_year="2022", geo="councildist", boundary_year="2023"), message = "boundary_year input used for var_codes parameter", class = "simpleMessage")

})

37 changes: 22 additions & 15 deletions vignettes/councilverse.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ remotes::install_github("newyorkcitycouncil/councilverse",build_vignettes = TRUE
```{r results='hide', message=FALSE}
library(tidyverse)
library(leaflet)
library(ggplot2)
library(sf)
# load councilverse as the last package
# councilverse also loads councildown and councilcount
library(councilverse)
Expand All @@ -57,17 +59,18 @@ library(councilverse)
We will use `councilcount` data in our example of how to make plots and maps using `councilverse` functions.

```{r message=FALSE}
# use councilcount::get_census_variables to check available variables
census_vars <- get_census_variables()
# use councilcount::get_geo_estimates to read in council district data for '% work from home' using 2013 council boundaries
# borough, communitydist, councildist, nta, policeprct, schooldist geographies are available
council_geo <- get_geo_estimates("councildist", var_codes = "DP03_0024E", boundary_year = "2013")
# use councilcount::get_ACS_variables() to check available variables for the 2018-2022 5-Year American Community Survey.
census_vars <- get_ACS_variables("2022")
# use councilcount::get_geo_estimates to read in council district data for '% work from home' using 2022 data and 2013 council boundaries
# 'borough', 'communitydist', 'councildist', 'nta', 'policeprct', and 'schooldist' geographies are available, along with citywide numbers (use 'city' as input)
council_geo <- get_geo_estimates(acs_year = "2022", geo = "councildist", var_codes = "DP03_0024E", boundary_year = "2013")
```

#### Visualize Data and Determine Map Breaks

```{r}
# check distribution of data (basic plot, without councildown)
names(council_geo)[names(council_geo) == 'DP03_0024PE'] <- 'percent_work_from_home_workers_16_and_older'
wfh_hist <- ggplot(data = council_geo, aes(percent_work_from_home_workers_16_and_older)) + geom_histogram(bins = 30)
# use fisher-jenks natural breaks after visualizing data
nat_intvl = classInt::classIntervals(council_geo$percent_work_from_home_workers_16_and_older, n = 5, style = 'fisher')
Expand Down Expand Up @@ -115,26 +118,30 @@ pal_council = colorBin(
```{r}
#| fig.alt: >
#| Add alt text
# tranform
council_geo <- st_transform(council_geo, crs = 4326)
#build map
map <- leaflet() %>%
# use councildown::addCouncilStyle() for leaflet styling
# Use councildown::addCouncilStyle() for leaflet styling
addCouncilStyle() %>%
# use the councildown::addPolygons default smoothFactor and weight
# Use the councildown::addPolygons default smoothFactor and weight
addPolygons(data = council_geo,
fillColor = ~pal_council(percent_work_from_home_workers_16_and_older),
fillOpacity = 1,
# use councildown::councilPopup to style popups
# Use councildown::councilPopup to style popups
label = ~lapply(councilPopup(
paste0("<h3>", paste0("CD: ",councildist13), "</h3>",
paste0("<h3>", paste0("CD: ", councildist), "</h3>",
"<p>", paste0("Work From Home: ", percent_work_from_home_workers_16_and_older, "%"), "</p>")),
htmltools::HTML)) %>%
# use councildown::addLegend_decreasing to reverse the ordering of the legend
# Use councildown::addLegend_decreasing to reverse the ordering of the legend
addLegend_decreasing(position = "topleft", pal = pal_council,
title = paste0("% Work From Home"),
values = c(0,1), opacity = 1, decreasing = T,
values = c(0, 1), opacity = 1, decreasing = TRUE,
labFormat = labelFormat(suffix = "%"),
na.label = "NA") %>%
# use councildown::addSourceText to add source text on bottom right of map
# Use councildown::addSourceText to add source text on the bottom right of the map
addSourceText(lat = -73.75, lon = 40.53, "Source: NYCC Data Team")

```

```{r dpi=300, out.width="100%"}
Expand Down Expand Up @@ -187,11 +194,11 @@ htmlwidgets::saveWidget(map, file=glue::glue("visuals/{map_html}"), selfcontaine

#### Population Estimates

`get_bbl_estimates()` -- Generates a dataframe that provides population estimates at the point level (there are also columns for various other geographies, like council district)
`get_bbl_estimates()` -- Generates a dataframe that provides population estimates at the BBL level. There are also columns for latitude and longitude, which allow for spatial joins if aggregation to other geography levels is desired.

`get_census_variables()` -- Provides information on all of the ACS demographic variables that can be accessed using `get_geo_estimates()` via variable codes
`get_ACS_variables()` -- Provides information on all of the available ACS demographic variables that can be accessed via get_geo_estimates() for a specified survey year.

`get_geo_estimates()` -- Creates a dataframe that provides population estimates for selected demographic variables along chosen geographic boundaries (e.g. council district, borough, etc.)
`get_geo_estimates()` -- Creates a dataframe that provides population estimates for selected demographic variables along chosen geographic boundaries (e.g. council district, borough, etc.) for a chosen ACS 5-Year survey.

### CouncilDown

Expand Down
Loading
Loading