Skip to content

Commit

Permalink
Add function to load FIPS data (#3)
Browse files Browse the repository at this point in the history
* Added `fips_data` function to load FIPS data from included csv files
* Provides cleaner way to load FIPS data in `usmap` package without
loading `extdata` file

See
[pdil/usmap#58](pdil/usmap#72 (comment))
  • Loading branch information
pdil committed Dec 11, 2023
2 parents a807c36 + 29c4a6c commit 5d78dd9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(centroid_labels)
export(fips_data)
export(us_map)
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
29 changes: 29 additions & 0 deletions R/fips-data.R
Original file line number Diff line number Diff line change
@@ -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
)
}
26 changes: 26 additions & 0 deletions man/fips_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions tests/testthat/test-fips-data.R
Original file line number Diff line number Diff line change
@@ -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")
})

0 comments on commit 5d78dd9

Please sign in to comment.