From c6079eb51525bcc758450e163f7c70bac9c0442d Mon Sep 17 00:00:00 2001 From: Jared Knowles Date: Thu, 20 Jul 2023 14:51:04 -0400 Subject: [PATCH] working build --- DESCRIPTION | 3 ++- Dockerfile | 2 +- NAMESPACE | 5 ++++- R/join_utilities.R | 2 +- R/utils.R | 19 +++++++++++++++---- man/match_test.Rd | 3 +++ man/perturb_count.Rd | 3 +++ man/postcode_lookup.Rd | 20 ++++++++++++++++++++ tests/testthat/test_utils.R | 2 +- 9 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 man/postcode_lookup.Rd diff --git a/DESCRIPTION b/DESCRIPTION index b1d7c82..46ada69 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,7 +17,8 @@ Imports: stringr, gridExtra, tidycensus, - grid + grid, + stringdist Encoding: UTF-8 LazyData: true Suggests: diff --git a/Dockerfile b/Dockerfile index b6447a8..b4d161e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,4 +19,4 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ && rm -rf /var/lib/apt/lists/* \ && mkdir -p /var/lib/shiny-server/bookmarks/shiny -RUN install2.r ggplot2 jpeg png stringr gridExtra grid testthat covr tidycensus +RUN install2.r ggplot2 jpeg png stringr gridExtra grid testthat covr tidycensus stringdist diff --git a/NAMESPACE b/NAMESPACE index 9ab0e70..133c5c1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,6 +21,7 @@ export(nvals) export(outersect) export(perturb_count) export(plot_jpeg) +export(postcode_lookup) export(pretty_count) export(pretty_per) export(race_short_names) @@ -33,6 +34,7 @@ export(theme_civilytics) export(z_gap_test) export(z_univariate) import(ggplot2) +import(tidycensus) importFrom(ggplot2,theme) importFrom(graphics,plot) importFrom(graphics,rasterImage) @@ -41,5 +43,6 @@ importFrom(grid,rasterGrob) importFrom(gridExtra,arrangeGrob) importFrom(jpeg,readJPEG) importFrom(png,readPNG) +importFrom(stats,runif) +importFrom(stringdist,stringsim) importFrom(stringr,str_count) -importFrom(tidycensus,fips_codes) diff --git a/R/join_utilities.R b/R/join_utilities.R index 98055f0..e0bab71 100644 --- a/R/join_utilities.R +++ b/R/join_utilities.R @@ -5,7 +5,7 @@ #' @param y a vector of identifiers to check against x #' @param distinct logical, should duplicate values of x and y be removed before testing #' -#' @return +#' @return nothing, print a summary of match statistics to the console #' @export #' #' @examples diff --git a/R/utils.R b/R/utils.R index a73694b..008d449 100644 --- a/R/utils.R +++ b/R/utils.R @@ -158,7 +158,7 @@ race_short_names <- function(x) { #' na_sum(x) # 15 na_sum <- function(x) { stopifnot(is.numeric(x)) - message("Taking a sum with missing values equal to 0, be careful 🐲") + message("Taking a sum with missing values equal to 0, be careful! \u1F601") x <- na_zero(x) return(sum(x)) } @@ -168,6 +168,15 @@ na_sum <- function(x) { # It also substitutes in PR and DC for Puerto Rico and District of Columbia # which are not included in the lookup table of states and state abbreviations # that comes with R. +#' Title +#' +#' @param x a vector of state names +## #' @importFrom datasets state.abb state.name +#' @return state abbreviations matching state naems provided in X +#' @export +#' +#' @examples +#' postcode_lookup("Montana") postcode_lookup <- function(x) { modify_name <- c(state.name, "District of Columbia", "Puerto Rico") modify_abb <- c(state.abb, "DC", "PR") @@ -181,6 +190,7 @@ postcode_lookup <- function(x) { #' @param x, the character value to match #' @param y, a vector of multiple character values to look for a match in #' @param n, an integer, how many matches to return +#' @importFrom stringdist stringsim #' #' @return an integer giving the position of the table with the most characters trunc_match <- function(x, y, n) { @@ -224,8 +234,8 @@ outersect <- function(x, y, ...) { #' @param stabbr a two letter abbreviation for a US state #' #' @return FIPS codes that match the abbreviation +#' @import tidycensus #' @export -#' @importFrom tidycensus fips_codes #' #' @examples #' get_fips("MT") @@ -243,8 +253,8 @@ get_fips <- function(stabbr) { #' @param fips a character value that captures the FIPS code with leading 0 #' #' @return a character value, length 2, with the state abbreviation +#' @import tidycensus #' @export -#' @importFrom tidycensus fips_codes #' #' @examples #' get_stabbr("06") @@ -339,7 +349,7 @@ z_univariate <- function(unit_prop, global_prop, unit_denom) { #' @details The count in the name means that this function enforces a floor of #' 0 on values, so values perturbed to have less than 0 will be capped at 0. #' -#' @return +#' @return a numeric vector #' @export #' #' @examples @@ -361,6 +371,7 @@ perturb_count <- function(x, fac = 3) { #' @return rounded values #' @export #' @details Credit to Jens von Bergmann for this algo https://github.com/mountainMath/dotdensity/blob/master/R/dot-density.R +#' @importFrom stats runif #' #' @examples #' random_round(1.93) diff --git a/man/match_test.Rd b/man/match_test.Rd index 0300d45..fd93b26 100644 --- a/man/match_test.Rd +++ b/man/match_test.Rd @@ -13,6 +13,9 @@ match_test(x, y, distinct = TRUE) \item{distinct}{logical, should duplicate values of x and y be removed before testing} } +\value{ +nothing, print a summary of match statistics to the console +} \description{ Test the join between two sets of identifiers } diff --git a/man/perturb_count.Rd b/man/perturb_count.Rd index 84b282f..5f78f8f 100644 --- a/man/perturb_count.Rd +++ b/man/perturb_count.Rd @@ -11,6 +11,9 @@ perturb_count(x, fac = 3) \item{fac}{the range of values to add or subtract to perturb the count} } +\value{ +a numeric vector +} \description{ Add a random jitter to a count variable to mask its true value } diff --git a/man/postcode_lookup.Rd b/man/postcode_lookup.Rd new file mode 100644 index 0000000..02da008 --- /dev/null +++ b/man/postcode_lookup.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{postcode_lookup} +\alias{postcode_lookup} +\title{Title} +\usage{ +postcode_lookup(x) +} +\arguments{ +\item{x}{a vector of state names} +} +\value{ +state abbreviations matching state naems provided in X +} +\description{ +Title +} +\examples{ +postcode_lookup("Montana") +} diff --git a/tests/testthat/test_utils.R b/tests/testthat/test_utils.R index 0ca09b5..5ffc6f4 100644 --- a/tests/testthat/test_utils.R +++ b/tests/testthat/test_utils.R @@ -50,7 +50,7 @@ test_that("Function subs out NAs in numeric vectors with 0", { # Test that na_sum works test_that("NA Sum takes sum setting NA values to 0", { expect_equivalent(na_sum(c(1:10, NA)), sum(1:10, 0)) - expect_message(na_sum(c(1:10, NA)), "Taking a sum with missing values equal to 0, be careful 🐲") + expect_message(na_sum(c(1:10, NA)), "Taking a sum with missing values equal to 0, be careful! \u1F601") }) test_that("na_sum fails with non-numerics", {