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/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. diff --git a/R/fips-data.R b/R/fips-data.R new file mode 100644 index 0000000..7e18ad9 --- /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 = "usmapdata"), + colClasses = rep("character", 3), stringsAsFactors = FALSE + ) + else if (regions_ == "county" || regions_ == "counties") + utils::read.csv( + system.file("extdata", "county_fips.csv", package = "usmapdata"), + 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..dcd4e87 --- /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]), 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[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") +})