Skip to content

Commit

Permalink
print addr objects in color and with style to visually represent tags (
Browse files Browse the repository at this point in the history
  • Loading branch information
cole-brokamp authored Oct 22, 2024
1 parent 69f4e1e commit ba14d7c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 97 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-0289-3151"))
Expand All @@ -18,6 +18,7 @@ Suggests:
tidyr
Imports:
purrr,
cli,
stringr,
dplyr,
glue,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 19 additions & 4 deletions R/addr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
29 changes: 12 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ library(addr)

``` r
addr(c("3333 Burnet Ave Cincinnati OH 45229", "202 Riva Ridge Ct Cincinnati OH 45140"))
#> <addr[2]>
#> [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
Expand Down Expand Up @@ -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)
#> <addr[3]>
#> [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
Expand Down Expand Up @@ -163,15 +158,15 @@ addr_match_geocode(x = sample(voter_addresses(), 100),
#> # A tibble: 100 × 3
#> addr s2 match_method
#> <addr> <s2cell> <fct>
#> 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
```
75 changes: 0 additions & 75 deletions inst/addr_and_s2.R

This file was deleted.

0 comments on commit ba14d7c

Please sign in to comment.