From ba14d7c33b399b801f7df99151f6a1bd585cf00b Mon Sep 17 00:00:00 2001 From: Cole Brokamp Date: Tue, 22 Oct 2024 13:27:11 -0400 Subject: [PATCH] print addr objects in color and with style to visually represent tags (#34) --- DESCRIPTION | 3 +- NAMESPACE | 1 + R/addr.R | 23 +++++++++++--- README.md | 29 ++++++++---------- inst/addr_and_s2.R | 75 ---------------------------------------------- 5 files changed, 34 insertions(+), 97 deletions(-) delete mode 100644 inst/addr_and_s2.R diff --git a/DESCRIPTION b/DESCRIPTION index 5532f09..0866bd5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: addr Title: Clean, Parse, Harmonize, Match, and Geocode Messy Real-World Addresses -Version: 0.4.0.9010 +Version: 0.4.0.9020 Authors@R: person("Cole", "Brokamp", , "cole@colebrokamp.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-0289-3151")) @@ -18,6 +18,7 @@ Suggests: tidyr Imports: purrr, + cli, stringr, dplyr, glue, diff --git a/NAMESPACE b/NAMESPACE index e94031a..2e9350b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ S3method(as.character,addr) S3method(as.data.frame,addr) S3method(format,addr) +S3method(print,addr) S3method(vec_ptype_abbr,addr) S3method(vec_ptype_full,addr) export(addr) diff --git a/R/addr.R b/R/addr.R index 4b74fa2..2d2fddd 100644 --- a/R/addr.R +++ b/R/addr.R @@ -15,7 +15,7 @@ #' In addition to the cleaning steps described in the arguments, the street number is coerced #' to a numeric after removing non-numeric characters. #' See `addr_tag()` for details on address component tagging. -#' +#' #' In the case of an address having more than one word for a tag (e.g., "Riva Ridge" for `StreetName`), #' then these are concatenated together, separated by a space in the order they appeared in the address. #' @param x a character vector of address strings @@ -53,8 +53,10 @@ addr <- function(x = character(), toi$street_name <- stringr::str_replace_all( toi$street_name, - stringr::regex(c("^east " = "e ", "^west " = "w ", "^north " = "n ", "^south " = "s ", - " east " = " e ", " west " = " w ", " north " = " n ", " south " = " s "), ignore_case = TRUE) + stringr::regex(c( + "^east " = "e ", "^west " = "w ", "^north " = "n ", "^south " = "s ", + " east " = " e ", " west " = " w ", " north " = " n ", " south " = " s " + ), ignore_case = TRUE) ) } if (expand_street_type) { @@ -128,7 +130,20 @@ methods::setOldClass(c("addr", "vctrs_vctr")) #' @export format.addr <- function(x, ...) { - as.character(x) + xd <- vctrs::vec_data(x) + paste( + cli::style_bold(cli::col_magenta(xd$street_number)), + cli::style_underline(cli::col_red(stringr::str_to_title(xd$street_name))), + cli::style_dim(cli::col_red(stringr::str_to_title(xd$street_type))), + cli::style_underline(cli::col_grey(stringr::str_to_title(xd$city))), + cli::style_italic(cli::col_grey(stringr::str_to_upper(xd$state))), + cli::style_underline(cli::col_blue(xd$zip_code)) + ) +} + +#' @export +print.addr <- function(x, ...) { + cat(format(x), "\n") } #' @export diff --git a/README.md b/README.md index 80299ce..c9eecb1 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,7 @@ library(addr) ``` r addr(c("3333 Burnet Ave Cincinnati OH 45229", "202 Riva Ridge Ct Cincinnati OH 45140")) -#> -#> [1] 3333 Burnet Avenue Cincinnati OH 45229 -#> [2] 202 Riva Ridge Court Cincinnati OH 45140 +#> 3333 Burnet Avenue Cincinnati OH 45229 202 Riva Ridge Court Cincinnati OH 45140 ``` Under the hood, an `addr` vector keeps a record of the tagged and @@ -83,10 +81,7 @@ addr(c("3333 Burnet Ave Cincinnati OH 45229", "5131 RAPID RUN RD CINCINNATI OHIO 45238" )) |> addr_match(cagis_addr()$cagis_addr) -#> -#> [1] 3333 Burnet Avenue Cincinnati OH 45229 -#> [2] 5130 Rapid Run Road Delhi Township OH 45238 -#> [3] NA +#> 3333 Burnet Avenue Cincinnati OH 45229 5130 Rapid Run Road Delhi Township OH 45238 NA NA NA NA NA NA ``` Use the matched addr vector to merge in address-specific data in the @@ -163,15 +158,15 @@ addr_match_geocode(x = sample(voter_addresses(), 100), #> # A tibble: 100 × 3 #> addr s2 match_method #> -#> 1 411 Pedretti Avenue Cincinnati OH 45238 8841b6192539a2e5 ref_addr -#> 2 7432 Bayswater Drive Cincinnati OH 45255 8841af3264f87405 tiger_range -#> 3 5366 Dickens Drive Cincinnati OH 45241 884051140ea170a7 ref_addr -#> 4 245 Mcguire Lane Cincinnati OH 45215 88405286586bc095 ref_addr -#> 5 3909 Dickson Avenue Cincinnati OH 45229 8841b30e55fab54d ref_addr -#> 6 4025 Egbert Avenue Cincinnati OH 45220 8841b37c962f5a3b ref_addr -#> 7 11451 Folkstone Drive Cincinnati OH 45240 88404ebd7161497d ref_addr -#> 8 1860 Queen City Avenue Cincinnati OH 45214 8841b43472fe3997 ref_addr -#> 9 9797 Cooper Woods Court Cincinnati OH 45241 88405251fbc59f91 ref_addr -#> 10 3815 Lincoln Road Cincinnati OH 45247 88403554febe6d93 ref_addr +#> 1 6971 Warder Drive Cincinnati OH 45224 88404ca4593038b7 ref_addr +#> 2 894 Woodshire Drive Cincinnati OH 45233 8841c97f9ee596d3 ref_addr +#> 3 269 Fleming Road Cincinnati OH 45215 88404c542fe43b4f ref_addr +#> 4 846 Oakfield Avenue Cincinnati OH 45224 88404ca8c2b81cc1 ref_addr +#> 5 6248 Elkwater Court Cincinnati OH 45248 8841cbb1da9b3963 ref_addr +#> 6 7740 Bowen Avenue Cincinnati OH 45255 8841a939f7c04e25 tiger_range +#> 7 6622 Abell Court Cincinnati OH 45247 884034efad1f9595 ref_addr +#> 8 3185 Jackfrost Way Cincinnati OH 45251 88404a3fb4362e93 ref_addr +#> 9 2577 Williamsburg Drive Cincinnati OH 45225 8841b4fe6c8193cd ref_addr +#> 10 NA NA W Mill St Cleves OH 45002 NA none #> # ℹ 90 more rows ``` diff --git a/inst/addr_and_s2.R b/inst/addr_and_s2.R deleted file mode 100644 index 54833be..0000000 --- a/inst/addr_and_s2.R +++ /dev/null @@ -1,75 +0,0 @@ -devtools::load_all() -library(s2) - -cagis_s2 <- - cagis_addr()$cagis_addr_data |> - purrr::modify_if(\(.) length(.) > 0 && nrow(.) > 1, dplyr::slice_sample, n = 1) |> - purrr::map_vec(purrr::pluck, "cagis_s2", .default = NA, .ptype = s2::s2_cell()) - -set.seed(1) -d <- addr_match_geocode( - x = sample(voter_addresses(), 200), - ref_addr = cagis_addr()$cagis_addr, - ref_s2 = cagis_s2, - county = "39061", - year = "2022" -) - -table(d$match_method) - -d <- - dplyr::filter(d, match_method %in% c("ref_addr", "tiger_range")) - -d$coarse_s2 <- s2_cell_parent(d$s2, 15) -d$coarse_s2_char <- as.character(d$coarse_s2) - -# median area in sq km -median(s2_cell_area_approx(d$coarse_s2)) / 1000000 - -d$geometry <- sf::st_as_sfc(s2_cell_center(d$s2)) - -bg <- - get_tiger_block_groups("39", "2022") |> - dplyr::filter(substr(GEOID, 1, 5) == "39061") |> - dplyr::mutate(geometry = sf::st_as_sfc(s2_geography)) - -s2_plot(bg$s2_geography, border = codec::codec_colors("grey blue"), col = codec::codec_colors("white")) -s2::s2_plot(s2_cell_to_lnglat(d$s2), add = TRUE, col = codec::codec_colors("darkish blue"), pch = 19) - -library(rdeck) - -rdeck(map_style = sprintf("mapbox://styles/mapbox/light-v%d", 10), initial_bounds = sf::st_bbox(bg$geometry)) |> - add_polygon_layer( - data = bg, - name = "block groups", - get_polygon = geometry, - opacity = 0.2, - filled = TRUE, - stroked = TRUE, - get_fill_color = codec::codec_colors("white"), - get_line_color = codec::codec_colors("grey blue"), - get_line_width = 50, - visible = TRUE, - visibility_toggle = FALSE, - pickable = FALSE - ) |> - ## add_polygon_layer( - ## data = d, - ## name = "s2_cell_outlines", - ## get_polygon = coarse_geo - ## ) |> - add_s2_layer( - data = d, - name = "s2_cell_layer", - get_s2_token = coarse_s2_char, - opacity = 0.5, - get_fill_color = codec::codec_colors("red") - ) |> - add_scatterplot_layer( - name = "s2_cell_centers", - data = d, - opacity = 0.5, - get_radius = 50, - get_position = geometry, - get_fill_color = codec::codec_colors("orange"), - )