Skip to content

Commit

Permalink
Merge pull request #56 from Metropolitan-Council/patch-rlang-ggplot2
Browse files Browse the repository at this point in the history
Patch rlang ggplot2
  • Loading branch information
eroten authored Mar 15, 2023
2 parents 41c0916 + 2b9db76 commit 9fc75dc
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 60 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: councilR
Title: Functions and Templates for the Metropolitan Council
Version: 0.2.0
Date: 2023-03-10
Version: 0.2.1
Date: 2023-03-15
Authors@R: c(
person("Metropolitan Council", role = "cph"),
person("Liz", "Roten", , "[email protected]", role = c("cre", "aut"),
Expand Down Expand Up @@ -32,7 +32,7 @@ Imports:
magrittr (>= 1.5),
odbc (>= 1.2.2),
purrr (>= 0.3),
rlang (>= 0.4.12),
rlang (>= 1.1.0),
rstudioapi (>= 0.11),
sf (>= 0.9.5),
tictoc (>= 1.0),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ importFrom(DBI,dbConnect)
importFrom(DBI,dbDisconnect)
importFrom(DBI,dbGetQuery)
importFrom(cli,cli_abort)
importFrom(cli,cli_inform)
importFrom(data.table,":=")
importFrom(data.table,.BY)
importFrom(data.table,.EACHI)
Expand Down
91 changes: 45 additions & 46 deletions R/fetch_county_geo.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,30 @@
#' @importFrom purrr map
#' @importFrom dplyr case_when mutate transmute
#'


fetch_county_geo <- function(core = TRUE, ...) {
rlang:::check_bool(core)

county_list <- if (core == TRUE) {
c(
"003", # "Anoka",
"019", # "Carver",
"037", # "Dakota",
"053", # "Hennepin",
"123", # "Ramsey",
"139", # "Scott",
"163" # "Washington"
list(
"Anoka County" = "003", # "Anoka",
"Carver County" = "019", # "Carver",
"Dakota County" = "037", # "Dakota",
"Hennepin County" = "053", # "Hennepin",
"Ramsey County" = "123", # "Ramsey",
"Scott County" = "139", # "Scott",
"Washington County" = "163" # "Washington"
)
} else if (core == FALSE) {
c(
"003", # "Anoka",
"019", # "Carver",
"037", # "Dakota",
"053", # "Hennepin",
"123", # "Ramsey",
"139", # "Scott",
"163", # "Washington"
"141", # "Sherburne",
"171" # "Wright"
list(
"Anoka County" = "003", # "Anoka",
"Carver County" = "019", # "Carver",
"Dakota County" = "037", # "Dakota",
"Hennepin County" = "053", # "Hennepin",
"Ramsey County" = "123", # "Ramsey",
"Scott County" = "139", # "Scott",
"Washington County" = "163", # "Washington"
"Sherburne County" = "141", # "Sherburne",
"Wright County" = "171" # "Wright"
)
}

Expand All @@ -85,26 +83,26 @@ fetch_ctu_geo <- function(core = TRUE, ...) {
NAME <- CTU_NAME <- ALAND <- AWATER <- NULL

county_list <- if (core == TRUE) {
c(
"003", # "Anoka",
"019", # "Carver",
"037", # "Dakota",
"053", # "Hennepin",
"123", # "Ramsey",
"139", # "Scott",
"163" # "Washington"
list(
"Anoka County" = "003", # "Anoka",
"Carver County" = "019", # "Carver",
"Dakota County" = "037", # "Dakota",
"Hennepin County" = "053", # "Hennepin",
"Ramsey County" = "123", # "Ramsey",
"Scott County" = "139", # "Scott",
"Washington County" = "163" # "Washington"
)
} else if (core == FALSE) {
c(
"003", # "Anoka",
"019", # "Carver",
"037", # "Dakota",
"053", # "Hennepin",
"123", # "Ramsey",
"139", # "Scott",
"163", # "Washington"
"141", # "Sherburne",
"171" # "Wright"
list(
"Anoka County" = "003", # "Anoka",
"Carver County" = "019", # "Carver",
"Dakota County" = "037", # "Dakota",
"Hennepin County" = "053", # "Hennepin",
"Ramsey County" = "123", # "Ramsey",
"Scott County" = "139", # "Scott",
"Washington County" = "163", # "Washington"
"Sherburne County" = "141", # "Sherburne",
"Wright County" = "171" # "Wright"
)
}

Expand All @@ -120,6 +118,12 @@ fetch_ctu_geo <- function(core = TRUE, ...) {
LSAD == 46 ~ paste(NAME, "(unorg.)"),
TRUE ~ NAME
)
) %>%
dplyr::left_join(mn_fips_codes,
by = c(
"COUNTYFP" = "county_code",
"STATEFP" = "state_code"
)
)

cities <- if (core == TRUE) {
Expand All @@ -133,21 +137,16 @@ fetch_ctu_geo <- function(core = TRUE, ...) {
cities_geo %>%
dplyr::group_by(NAME) %>%
dplyr::mutate(n = dplyr::n()) %>%
dplyr::left_join(sf::st_drop_geometry(cities_geo) %>%
dplyr::transmute(
COUNTYFP = COUNTYFP,
CONAME = NAME
)) %>%
dplyr::mutate(CTU_NAME = dplyr::if_else(
n > 1 & LSAD != 25,
paste0(NAME, " - ", CONAME, " Co."), # cities dont get merged
paste0(NAME, " - ", county, " Co."), # cities dont get merged
NAME
)) %>%
dplyr::group_by(CTU_NAME) %>%
dplyr::summarise(
geometry = sf::st_union(geometry),
ALAND = sum(ALAND, na.rm = T),
AWATER = sum(AWATER, na.rm = T)
ALAND = sum(ALAND, na.rm = TRUE),
AWATER = sum(AWATER, na.rm = TRUE)
) %>%
dplyr::arrange(CTU_NAME)
}
Expand Down
2 changes: 1 addition & 1 deletion R/import_from_gpkg.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import_from_gpkg <- function(link,
rlang:::check_bool
)
rlang:::check_string(save_path)
rlang:::check_number(.crs)
rlang:::check_number_whole(.crs)

# download to a temp file
temp <- tempfile()
Expand Down
3 changes: 2 additions & 1 deletion R/look.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#'
#' @rdname look
#' @export
#' @importFrom cli cli_inform
#'
look <- function(var, n = 1, unique = TRUE) {
argname <- sys.call()[2]
Expand All @@ -37,6 +38,6 @@ look <- function(var, n = 1, unique = TRUE) {
} else {
sampleVar <- sample(var, n)
}
message(argname, " = ", paste(sampleVar, collapse = ""))
cli::cli_inform(c(argname, " = ", paste(sampleVar, collapse = "")))
var %in% sampleVar
}
19 changes: 19 additions & 0 deletions R/mn_fips_codes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' @title Minnesota county FIPS codes
#'
#' @description A data.frame with county codes and names for the
#' state of Minnesota. Derived from `{tigris}` fips_codes.
#'
#' @format A data.frame with 5 columns and 87 rows
#' \describe{
#' \item{state}{character, state abbreviation}
#' \item{state_code}{character, two digit state code}
#' \item{state_name}{character, full state name}
#' \item{county_code}{character, three digit county code}
#' \item{county}{character, full county name}
#' }
#'
#' @family data
#' @examples
#' mn_fips_codes
#' @source `{tigris}` dataset, `fips_codes`.
"mn_fips_codes"
8 changes: 4 additions & 4 deletions R/theme_council.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ theme_council <- function(base_size = 11,

purrr::map(
c(base_size),
rlang:::check_number
rlang:::check_number_decimal
)

rlang:::check_string(base_family)
Expand Down Expand Up @@ -190,14 +190,14 @@ theme_council <- function(base_size = 11,
# SETUP -----
line = ggplot2::element_line(
colour = colors$suppBlack,
size = base_line_size,
linewidth = base_line_size,
linetype = 1,
lineend = "butt"
),
rect = ggplot2::element_rect(
fill = colors$suppWhite,
colour = colors$suppBlack,
size = base_rect_size,
linewidth = base_rect_size,
linetype = 1
),
text = ggplot2::element_text(
Expand Down Expand Up @@ -335,7 +335,7 @@ theme_council <- function(base_size = 11,
panel.border = ggplot2::element_blank(),
panel.grid = ggplot2::element_line(colour = "grey92"),
panel.grid.minor = ggplot2::element_blank(),
panel.grid.major = ggplot2::element_line(size = ggplot2::rel(1)),
panel.grid.major = ggplot2::element_line(linewidth = ggplot2::rel(1)),
panel.spacing = ggplot2::unit(half_line, "pt"),
panel.spacing.x = NULL,
panel.spacing.y = NULL,
Expand Down
9 changes: 9 additions & 0 deletions data-raw/mn_fips_codes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## code to prepare `mn_fips_codes` dataset goes here

load_all()

mn_fips_codes <- tigris::fips_codes %>%
dplyr::filter(state_code == "27")


usethis::use_data(mn_fips_codes, overwrite = TRUE)
Binary file added data/mn_fips_codes.rda
Binary file not shown.
31 changes: 31 additions & 0 deletions man/mn_fips_codes.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-fetch_ctu_geo.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("non T/F core fails", {
test_that("non TRUE/FALSE core fails", {
testthat::expect_error(fetch_ctu_geo(core = "foo"))
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-import_from_emissions.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ testthat::skip_if(httr2::secret_has_key("QUHBRb_yoy2RRj59qno8NVXA7mW402xkins"))
testthat::test_that("Residential electricity data is returned", {
electric_residential <- import_from_emissions(
uid = httr2::secret_decrypt("QUHBRb_yoy2RRj59qno8NVXA7mW402xkins", "COUNCILR_KEY"),
pwd = httr2::secret_decrypt("IMcfHFuibrH6IsuwIE_vd9oJmNTlOkpt", "COUNCILR_KEY"),
pwd = httr2::secret_decrypt("rQHk4S39pjfJ6yoKWUUNpQUDk2i9XA3d", "COUNCILR_KEY"),
table_name = "metro_energy.vw_electricity_residential_ctu"
)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-import_from_gis.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ testthat::skip_if(httr2::secret_has_key("QUHBRb_yoy2RRj59qno8NVXA7mW402xkins"))
testthat::test_that("airports spatial dataset", {
airport <- import_from_gis(
uid = httr2::secret_decrypt("QUHBRb_yoy2RRj59qno8NVXA7mW402xkins", "COUNCILR_KEY"),
pwd = httr2::secret_decrypt("IMcfHFuibrH6IsuwIE_vd9oJmNTlOkpt", "COUNCILR_KEY"),
pwd = httr2::secret_decrypt("rQHk4S39pjfJ6yoKWUUNpQUDk2i9XA3d", "COUNCILR_KEY"),
query = "GISLibrary.dbo.AIRPORTS",
dbname = "GISLibrary",
.quiet = TRUE
Expand Down
4 changes: 2 additions & 2 deletions vignettes/Spatial_helpers.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ You can also make a map quickly by using `map_council_continuous()`. This also a
```{r plot-county, eval = TRUE}
fetch_county_geo() %>%
map_council_continuous(.fill = ALAND) +
labs(title = "Counties")
ggplot2::labs(title = "Counties")
```


Expand All @@ -76,5 +76,5 @@ You can also fetch CTUs.
```{r plot-city, eval = TRUE}
fetch_ctu_geo() %>%
map_council_continuous(.fill = ALAND) +
labs(title = "CTUs")
ggplot2::labs(title = "CTUs")
```

0 comments on commit 9fc75dc

Please sign in to comment.