From 9934cacf73f54aa6da68ecfe569405bd322d4180 Mon Sep 17 00:00:00 2001 From: Paolo Di Lorenzo Date: Sun, 10 Dec 2023 21:05:35 -0500 Subject: [PATCH 1/3] Add function to load fips data --- NAMESPACE | 1 + R/fips-data.R | 29 +++++++++++++++++++++++ man/fips_data.Rd | 26 ++++++++++++++++++++ tests/testthat/test-fips-data.R | 42 +++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 R/fips-data.R create mode 100644 man/fips_data.Rd create mode 100644 tests/testthat/test-fips-data.R diff --git a/NAMESPACE b/NAMESPACE index f9a81a9..40fb9b8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ # Generated by roxygen2: do not edit by hand export(centroid_labels) +export(fips_data) export(us_map) diff --git a/R/fips-data.R b/R/fips-data.R new file mode 100644 index 0000000..4cf197f --- /dev/null +++ b/R/fips-data.R @@ -0,0 +1,29 @@ +#' Retrieve state and county FIPS codes +#' +#' @param regions The region breakdown for the map, can be one of +#' (\code{"states"}, \code{"state"}, \code{"counties"}, \code{"county"}). +#' The default is \code{"states"}. +#' +#' @return A data frame of FIPS codes of the desired \code{regions}. +#' +#' @examples +#' str(fips_data()) +#' +#' state_fips <- fips_data() +#' county_fips <- fips_data(regions = "counties") +#' +#' @export +fips_data <- function(regions = c("states", "state", "counties", "county")) { + regions_ <- match.arg(regions) + + if (regions_ == "state" || regions_ == "states") + utils::read.csv( + system.file("extdata", "state_fips.csv", package = "usmap"), + colClasses = rep("character", 3), stringsAsFactors = FALSE + ) + else if (regions_ == "county" || regions_ == "counties") + utils::read.csv( + system.file("extdata", "county_fips.csv", package = "usmap"), + colClasses = rep("character", 4), stringsAsFactors = FALSE + ) +} diff --git a/man/fips_data.Rd b/man/fips_data.Rd new file mode 100644 index 0000000..3d0f47b --- /dev/null +++ b/man/fips_data.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fips-data.R +\name{fips_data} +\alias{fips_data} +\title{Retrieve state and county FIPS codes} +\usage{ +fips_data(regions = c("states", "state", "counties", "county")) +} +\arguments{ +\item{regions}{The region breakdown for the map, can be one of +(\code{"states"}, \code{"state"}, \code{"counties"}, \code{"county"}). +The default is \code{"states"}.} +} +\value{ +A data frame of FIPS codes of the desired \code{regions}. +} +\description{ +Retrieve state and county FIPS codes +} +\examples{ +str(fips_data()) + +state_fips <- fips_data() +county_fips <- fips_data(regions = "counties") + +} diff --git a/tests/testthat/test-fips-data.R b/tests/testthat/test-fips-data.R new file mode 100644 index 0000000..3b37f22 --- /dev/null +++ b/tests/testthat/test-fips-data.R @@ -0,0 +1,42 @@ +context("Loading FIPS data") + +test_that("state FIPS codes load correctly", { + fips <- fips_data() + state_fips <- fips_data("state") + states_fips <- fips_data("states") + + expect_identical(fips, state_fips) + expect_identical(fips, states_fips) + expect_identical(state_fips, states_fips) + + expect_equal(length(fips), 3) + expect_equal(length(fips[, 1]), 51) + + expect_equal(fips[1, "abbr"], "AK") + expect_equal(fips[1, "fips"], "02") + expect_equal(fips[1, "full"], "Alaska") + + expect_equal(fips[51, "abbr"], "WY") + expect_equal(fips[51, "fips"], "56") + expect_equal(fips[51, "full"], "Wyoming") +}) + +test_that("county FIPS codes load correctly", { + county_fips <- fips_data("county") + counties_fips <- fips_data("counties") + + expect_identical(county_fips, counties_fips) + + expect_equal(length(county_fips), 4) + expect_equal(length(county_fips[, 1]), 3235) + + expect_equal(county_fips[1, "full"], "Alaska") + expect_equal(county_fips[1, "abbr"], "AK") + expect_equal(county_fips[1, "county"], "Aleutians West Census Area") + expect_equal(county_fips[1, "fips"], "02016") + + expect_equal(county_fips[3235, "full"], "Wyoming") + expect_equal(county_fips[3235, "abbr"], "WY") + expect_equal(county_fips[3235, "county"], "Washakie County") + expect_equal(county_fips[3235, "fips"], "56043") +}) From 3648a850b849ac828e0d120399a48b4c4a63ad80 Mon Sep 17 00:00:00 2001 From: Paolo Di Lorenzo Date: Sun, 10 Dec 2023 21:09:06 -0500 Subject: [PATCH 2/3] Update changelog with fips_data --- NEWS.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 475fbc9..a36356a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ # usmapdata 0.1.1.9999 - +* Add `fips_data` function to load raw FIPS data from included csv files. + * `fips_data()`, `fips_data("state")`, or `fips_data("states")` load state FIPS codes + * `fips_data("county")` or `fips_data("counties")` load county FIPS codes # usmapdata 0.1.1 Released Saturday, October 21, 2023. From b25a9a50d02d5c08df817d9c4206d62360260010 Mon Sep 17 00:00:00 2001 From: Paolo Di Lorenzo Date: Sun, 10 Dec 2023 21:16:11 -0500 Subject: [PATCH 3/3] Fix which package fips data is being loaded from --- R/fips-data.R | 4 ++-- tests/testthat/test-fips-data.R | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/R/fips-data.R b/R/fips-data.R index 4cf197f..7e18ad9 100644 --- a/R/fips-data.R +++ b/R/fips-data.R @@ -18,12 +18,12 @@ fips_data <- function(regions = c("states", "state", "counties", "county")) { if (regions_ == "state" || regions_ == "states") utils::read.csv( - system.file("extdata", "state_fips.csv", package = "usmap"), + system.file("extdata", "state_fips.csv", package = "usmapdata"), colClasses = rep("character", 3), stringsAsFactors = FALSE ) else if (regions_ == "county" || regions_ == "counties") utils::read.csv( - system.file("extdata", "county_fips.csv", package = "usmap"), + system.file("extdata", "county_fips.csv", package = "usmapdata"), colClasses = rep("character", 4), stringsAsFactors = FALSE ) } diff --git a/tests/testthat/test-fips-data.R b/tests/testthat/test-fips-data.R index 3b37f22..dcd4e87 100644 --- a/tests/testthat/test-fips-data.R +++ b/tests/testthat/test-fips-data.R @@ -28,15 +28,15 @@ test_that("county FIPS codes load correctly", { expect_identical(county_fips, counties_fips) expect_equal(length(county_fips), 4) - expect_equal(length(county_fips[, 1]), 3235) + expect_equal(length(county_fips[, 1]), 3236) expect_equal(county_fips[1, "full"], "Alaska") expect_equal(county_fips[1, "abbr"], "AK") expect_equal(county_fips[1, "county"], "Aleutians West Census Area") expect_equal(county_fips[1, "fips"], "02016") - expect_equal(county_fips[3235, "full"], "Wyoming") - expect_equal(county_fips[3235, "abbr"], "WY") - expect_equal(county_fips[3235, "county"], "Washakie County") - expect_equal(county_fips[3235, "fips"], "56043") + expect_equal(county_fips[3236, "full"], "Wyoming") + expect_equal(county_fips[3236, "abbr"], "WY") + expect_equal(county_fips[3236, "county"], "Washakie County") + expect_equal(county_fips[3236, "fips"], "56043") })