diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index e5a6da0..924b04c 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -1,7 +1,4 @@ # lintr provides static code analysis for R. -# It checks for adherence to a given style, -# identifying syntax errors and possible semantic issues, -# then reports them to you so you can take action. # More details at https://lintr.r-lib.org/ name: lint @@ -34,7 +31,7 @@ jobs: extra-packages: lintr - name: Run lintr - run: lintr::sarif_output(lintr::lint_dir("."), "lintr-results.sarif") + run: lintr::sarif_output(lintr::lint_package(), "lintr-results.sarif") shell: Rscript {0} continue-on-error: true diff --git a/DESCRIPTION b/DESCRIPTION index 81b5df1..e3a3c3a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: usmapdata Title: Mapping Data for 'usmap' Package -Version: 0.6.0.9000 +Version: 1.0.0.9000 Description: Provides a container for data used by the 'usmap' package. The data used by 'usmap' has been extracted into this package so that the file size of the 'usmap' package can be reduced greatly. The data in this diff --git a/NEWS.md b/NEWS.md index 59b956d..0857bf7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # [unreleased] +# usmapdata 1.0.0 +Released Friday, August 22, 2025. + ### New Features * Puerto Rico has been added! * Finally, after years of procrastinating, Puerto Rico has finally been added to the map, see [Issue #48](https://github.com/pdil/usmapdata/issues/48). diff --git a/R/usmapdata-package.R b/R/usmapdata-package.R index 816860f..60611ac 100644 --- a/R/usmapdata-package.R +++ b/R/usmapdata-package.R @@ -2,12 +2,12 @@ #' #' @description #' It is usually difficult or inconvenient to create US maps that -#' include both Alaska and Hawaii in a convenient spot. All map +#' include Alaska, Hawaii, and even Puerto Rico in a convenient spot. All map #' data frames produced by this package use the US National Atlas Equal Area #' projection. #' #' @section Map data frames: -#' Alaska and Hawaii have been manually moved to a new location so that +#' Alaska, Hawaii, and Puerto Rico have been manually moved to a new location so that #' their new coordinates place them to the bottom-left corner of #' the map. These maps can be accessed by using the \link{us_map} function. #' diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/DESCRIPTION b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/DESCRIPTION new file mode 100644 index 0000000..cd6487c --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/DESCRIPTION @@ -0,0 +1,24 @@ +Package: usmapdata +Title: Mapping Data for 'usmap' Package +Version: 0.6.0.9000 +Description: Provides a container for data used by the 'usmap' package. + The data used by 'usmap' has been extracted into this package so that the + file size of the 'usmap' package can be reduced greatly. The data in this + package will be updated roughly once per year as new map data files are + provided by the US Census Bureau. +Authors@R: person("Paolo", "Di Lorenzo", email = "paolo@dilorenzo.org", role = c("aut", "cre")) +Depends: R (>= 3.5.0) +License: GPL (>= 3) +Encoding: UTF-8 +Language: en-US +URL: https://usmap.dev +BugReports: https://github.com/pdil/usmapdata/issues +Imports: rlang, sf +Suggests: covr, dplyr, spelling, testthat (>= 3.0.0), withr +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.2 +Config/testthat/edition: 3 +NeedsCompilation: no +Packaged: 2025-08-21 03:25:18 UTC; runner +Author: Paolo Di Lorenzo [aut, cre] +Maintainer: Paolo Di Lorenzo diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/NAMESPACE b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/NAMESPACE new file mode 100644 index 0000000..e119770 --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/NAMESPACE @@ -0,0 +1,7 @@ +# Generated by roxygen2: do not edit by hand + +export(available_map_years) +export(centroid_labels) +export(fips_data) +export(us_map) +importFrom(rlang,.data) diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/NEWS.md b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/NEWS.md new file mode 100644 index 0000000..59b956d --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/NEWS.md @@ -0,0 +1,111 @@ +# [unreleased] + +### New Features +* Puerto Rico has been added! + * Finally, after years of procrastinating, Puerto Rico has finally been added to the map, see [Issue #48](https://github.com/pdil/usmapdata/issues/48). + * All included map files have been retroactively updated to include Puerto Rico, so any valid value of `data_year` will include Puerto Rico if desired. + * `us_map()` and `fips_data()` both return Puerto Rico in their data sets and it can be included or excluded just like any state (using FIPS, full name, abbreviation, etc.). + * Special thanks [@dcaud](https://github.com/dcaud) who started this work [years ago](https://github.com/pdil/usmap/pull/34). + +### Enhancements +* `include` now takes precedence over `exclude` in `us_map()`. + * Any items that are in both the `include` and `exclude` vectors will be _included_. + +### Removed +* The `as_sf` parameter has been completely removed from `us_map()`, `centroid_labels()`, and `fips_data()`. + * It was no longer used by `usmap` nor did it have any effect if set. + * Any existing code that sets it can safely delete it from `usmapdata` function calls. + +# usmapdata 0.6.0 +Released Saturday, June 14, 2025. + +* Change output of `us_map()` and `centroid_labels()` to data frame instead of tibble. + * If `tibble` format is required use `tibble::as_tibble()` on the output. + +# usmapdata 0.5.0 +Released Thursday, May 22, 2025. + +* Add [2024 shape files](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.2024.html#list-tab-1883739534). + +# usmapdata 0.4.0 +Released Thursday, March 6, 2025. + +### New Features +* Add `data_year` parameter to `us_map()`, see [Issue #34](https://github.com/pdil/usmapdata/issues/34). + * Allows user to select the year for which to plot US map. + * This will allow the user to match the map that is provided to the data they are using. + * To start with, 2021, 2022, and 2023 maps are included. + * Going forward, each year will be added to the package and previous years can be accessed with this parameter. + * If the value provided via `data_year` is not available, the package will select the next year for which data exists. + * For example, if data sets 2022 and 2023 are available and the user calls `us_map(data_year = 2019)`, 2022 will be used. + * A warning is presented when this occurs to alert the user. + * Further reading on the impetus for this change: [major changes made to Connecticut counties in 2023](https://www.ctinsider.com/projects/2023/ct-planning-regions/). + * The old Connecticut counties are available in the 2021 data, 2022 and forward use the new planning regions. + +### Improvements +* Improve python script and GitHub Actions workflow that download and process map shapefiles to be more flexible and support new `data_year` feature listed above. +* `centroid_labels()` now accepts `"state"` and `"county"` as inputs for the `regions` parameter like `us_map()` and `fips_data()`. +* Update package author email. + +# usmapdata 0.3.0 +Released Friday, May 15, 2024. + +* Update map data to use [2023 shape files](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.2023.html#list-tab-1883739534). + +# usmapdata 0.2.2 +Released Friday, March 8, 2024. + +### Improvements +* Improve language in `DESCRIPTION` and minor documentation, see [Issue #19](https://github.com/pdil/usmapdata/issues/19). + +### Bug Fixes +* `alaska_bbox()` and `hawaii_bbox()` now output correct `sf` type (`sfc_POLYGON`). + +# usmapdata 0.2.1 +Released Sunday, February 4, 2024. + +This update continues the `sf` migration by setting the `as_sf` parameter to default to the behavior of `TRUE`. This parameter no longer has any effect, as explained below. The next phase will involve updating `usmap` to no longer make use of this parameter, in which case it can be completely removed. + +### Removed +* The `as_sf` parameter is now deprecated and no longer has any effect. + * As part of this removal, the default behavior for `us_map()`, `centroid_labels()`, and `fips_data()` is equivalent to `as_sf = TRUE` which is to return their data as an `sf` object (see `0.2.0` release notes for more details). + * This parameter will be completely removed in a future version but continues to exist for compatibility reasons. +* Legacy `.csv` files containing mapping and FIPS data have been removed, greatly reducing package size. + +# usmapdata 0.2.0 +Released Friday, January 12, 2024. + +### Improvements +* Update map data to use [2022 shape files](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.2022.html#list-tab-1883739534). +* Begin process of upgrading map data to use GeoPackage files instead of csv. + * Previously the files were created using now-retired packages `rgdal`, `rgeos`, and `maptools`. + * The new files can be accessed by passing `as_sf = TRUE` to the `us_map()` and `fips_data()` functions. + * Once the upgrade is complete, this parameter will be removed and the new functionality will be the default. + * The new map files are smaller in size while maintaining the same resolution. + * The format of the data also allows for easier manipulation in the future using the `sf` package. +* Add scripts to perform automated map data updates, see [Issue #5](https://github.com/pdil/usmapdata/issues/5). + * The scripts will check for new shapefiles from the US Census Bureau twice a year and automatically update the data. + * The scripts can also be run manually as needed. + * Once data is updated a new `usmapdata` release will be created. + +# usmapdata 0.1.2 +Released Monday, December 11, 2023. + +* 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. + +* Update package author email and website. + +# usmapdata 0.1.0 +Released Wednesday, February 2, 2022. + +* First release + +### Main features + +* Contains the `us_map` function and associated data extracted from the `usmap` package +* Will allow future removal of data from `usmap` package so file size can be reduced greatly. diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/create-us-map.R b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/create-us-map.R new file mode 100644 index 0000000..8b2c15b --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/create-us-map.R @@ -0,0 +1,299 @@ +#' Internal map creation tools +#' +#' @description +#' `create_us_map()` creates the modified shapefiles used by the +#' \link[usmap]{usmap} package. +#' +#' `ea_crs()` returns the US National Atlas Equal Area coordinate reference system +#' (CRS) used by this package and \link[usmap]{usmap}. +#' +#' `transform2D()` computes a two dimensional affine transformation matrix +#' for the provided rotation angle and scale factor. +#' +#' `transform_alaska()` applies the appropriate transform for the Alaska polygons. +#' +#' `transform_hawaii()` applies the appropriate transform for the Hawaii polygons. +#' +#' `transform_puerto_rico()` applies the appropriate transform for the Puerto Rico polygons. +#' +#' `compute_centroids()` computes the modified centroids for each state or +#' county polygon using a center-of-mass technique on the largest polygon in +#' the region. +#' +#' `alaska_bbox()` returns the bounding box of Alaska pre-transformation. +#' +#' `hawaii_bbox()` returns the bounding box of Hawaii pre-transformation. +#' +#' `puerto_rico_bbox()` returns the bounding box of Puerto Rico pre-transformation. +#' +#' @note +#' Using these functions externally is not recommended since they make certain +#' undocumented assumptions that may not work with all inputs. +#' +#' It is strongly recommend that the \link[usmap]{usmap} package is used +#' directly. +#' +#' In some cases where the raw data is required, the \link{us_map} and +#' \link{centroid_labels} functions located in this package can be used instead. +#' +#' @references { +#' Gert (2017). “How to calculate +#' polygon centroids in R (for +#' non-contiguous shapes).” +#' . +#' +#' Rudis B (2014). “Moving The Earth +#' (well, Alaska & Hawaii) With R.” +#' . +#' } +#' +#' @keywords internal +create_us_map <- function( + type = c("states", "counties"), + input_file, + output_dir, + output_file +) { + # check for dplyr + if (!requireNamespace("dplyr", quietly = TRUE)) { + stop("`dplyr` must be installed to use `create_us_map()`. + Use: install.packages(\"dplyr\") and try again.") + } + + type <- match.arg(type) + + # import map file + us <- sf::read_sf(input_file, as_tibble = FALSE) + + # ea: US National Atlas Equal Area + us_ea <- sf::st_transform(us, ea_crs()) + + # FIPS code for Alaska = 02 + alaska <- transform_alaska(us_ea[us_ea$STATEFP == "02", ]) + + # FIPS code for Hawaii = 15 + hawaii <- transform_hawaii(us_ea[us_ea$STATEFP == "15", ]) + + # FIPS code for Puerto Rico = 72 + puerto_rico <- transform_puerto_rico(us_ea[us_ea$STATEFP == "72", ]) + + # keep only US states (i.e. remove territories, minor outlying islands, etc.) + # also remove Alaska (02), Hawaii (15), Puerto Rico (72) so that we can add in + # shifted versions + us_ea <- us_ea[!us_ea$STATEFP %in% c(as.character(57:80), "02", "15", "72"), ] + us_ea <- rbind(us_ea, alaska, hawaii, puerto_rico) + + # delete unused columns + cols <- c() + if (type == "states") { + cols <- c("GEOID", "STUSPS", "NAME") + } else if (type == "counties") { + cols <- c("GEOID", "STUSPS", "STATE_NAME", "NAMELSAD") + } + us_ea <- dplyr::select(us_ea, dplyr::all_of(cols)) + + # rename remaining columns + new_cols <- c() + if (type == "states") { + new_cols <- c(fips = "GEOID", abbr = "STUSPS", full = "NAME") + } else if (type == "counties") { + new_cols <- c(fips = "GEOID", abbr = "STUSPS", full = "STATE_NAME", county = "NAMELSAD") + } + us_ea <- dplyr::rename(us_ea, dplyr::all_of(new_cols)) + + # sort output + if (type == "states") { + us_ea <- dplyr::arrange(us_ea, .data$abbr) + } else if (type == "counties") { + us_ea <- dplyr::arrange(us_ea, .data$abbr, .data$county) + } + + # prepare output directory + if (!dir.exists(output_dir)) { + dir.create(output_dir, recursive = TRUE) + } + output_path <- file.path(output_dir, output_file) + + # export modified shape file + sf::st_write(us_ea, output_path, quiet = TRUE, append = FALSE) + + # compute centroids + centroids <- compute_centroids(us_ea) + + # determine centroids file path + centroids_output_path <- file.path( + output_dir, + paste0( + tools::file_path_sans_ext(basename(output_path)), + "_centroids.", + tools::file_ext(output_path) + ) + ) + + # export centroids + sf::st_write(centroids, centroids_output_path, quiet = TRUE, append = FALSE) +} + +#' @rdname create_us_map +#' @keywords internal +ea_crs <- function() { + sf::st_crs(9311) # US National Atlas Equal Area coordinate reference system +} + +#' @rdname create_us_map +#' @keywords internal +transform2D <- function(angle = 0, scale = 1) { + r <- angle * pi / 180 + matrix(c(scale * cos(r), scale * sin(r), + -scale * sin(r), scale * cos(r)), 2, 2) +} + +#' @rdname create_us_map +#' @keywords internal +transform_alaska <- function(alaska) { + sf::st_geometry(alaska) <- sf::st_geometry(alaska) * transform2D(-50, 1 / 2) + sf::st_geometry(alaska) <- sf::st_geometry(alaska) + c(3e5, -2e6) + sf::st_crs(alaska) <- ea_crs() + + alaska +} + +#' @rdname create_us_map +#' @keywords internal +transform_hawaii <- function(hawaii) { + sf::st_geometry(hawaii) <- sf::st_geometry(hawaii) * transform2D(-35) + sf::st_geometry(hawaii) <- sf::st_geometry(hawaii) + c(3.6e6, 1.8e6) + sf::st_crs(hawaii) <- ea_crs() + + hawaii +} + +#' @rdname create_us_map +#' @keywords internal +transform_puerto_rico <- function(puerto_rico) { + sf::st_geometry(puerto_rico) <- sf::st_geometry(puerto_rico) * transform2D(15, 2) + sf::st_geometry(puerto_rico) <- sf::st_geometry(puerto_rico) + c(-4.5e6, 4e6) + sf::st_crs(puerto_rico) <- ea_crs() + + puerto_rico +} + +#' @rdname create_us_map +#' @keywords internal +compute_centroids <- function(polygons, iterations = 3, initial_width_step = 10) { + if (iterations < 1) { + stop("`iterations` must be greater than or equal to 1.") + } + + if (initial_width_step < 1) { + stop("`initial_width_step` must be greater than or equal to 1.") + } + + new_polygons <- sf::st_as_sf(polygons) + + # Iterate through each provided polygon + for (i in seq_len(nrow(polygons))) { + width <- -initial_width_step + area <- as.numeric(sf::st_area(polygons[i, ])) + current_polygon <- polygons[i, ] + + isEmpty <- FALSE + for (j in 1:iterations) { + # Stop if buffer polygon becomes empty + if (!isEmpty) { + buffer <- sf::st_buffer(current_polygon, dist = width) + + # Repeatedly increase buffer size until non-empty if needed + subtract_width <- width / 20 + while (sf::st_is_empty(buffer)) { + width <- width - subtract_width + buffer <- sf::st_buffer(current_polygon, dist = width) + isEmpty <- TRUE + } + + new_area <- as.numeric(sf::st_area(buffer)) + + # Determine width needed to reduce area to 1/4 of current + # for next iteration + slope <- (new_area - area) / width + width <- (area / 4 - area) / slope + + # Set values for next iteration + area <- new_area + current_polygon <- buffer + } + } + + # Determine biggest polygon in case of multiple polygons + d <- sf::st_geometry(current_polygon) + + if (length(d) > 1) { + biggest_area <- sf::st_area(d[1, ]) + + which_polygon <- 1 + for (k in 2:length(d)) { + if (sf::st_area(d[k, ]) > biggest_area) { + biggest_area <- sf::st_area(d[k, ]) + which_polygon <- k + } + } + + current_polygon <- d[which_polygon, ] + } + + # Replace existing polygon with new polygon + new_polygons[i, ] <- current_polygon + } + + # Return centroids of newly computed polygons + sf::st_agr(new_polygons) <- "constant" + sf::st_centroid(new_polygons) +} + +#' @rdname create_us_map +#' @keywords internal +alaska_bbox <- function() { + sf::st_as_sfc( + sf::st_bbox( + c( + xmin = -4377000, + xmax = -1519000, + ymin = 1466000, + ymax = 3914000 + ), + crs = ea_crs() + ) + ) +} + +#' @rdname create_us_map +#' @keywords internal +hawaii_bbox <- function() { + sf::st_as_sfc( + sf::st_bbox( + c( + xmin = -5750000, + xmax = -5450000, + ymin = -1050000, + ymax = -441000 + ), + crs = ea_crs() + ) + ) +} + +#' @rdname create_us_map +#' @keywords internal +puerto_rico_bbox <- function() { + sf::st_as_sfc( + sf::st_bbox( + c( + xmin = 3300000, + xmax = 3700000, + ymin = -2400000, + ymax = -2200000 + ), + crs = ea_crs() + ) + ) +} diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/fips-data.R b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/fips-data.R new file mode 100644 index 0000000..0f7a748 --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/fips-data.R @@ -0,0 +1,23 @@ +#' Retrieve state and county FIPS codes +#' +#' @inheritParams us_map +#' +#' @return An data frame of FIPS codes of the desired `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"), + data_year = NULL +) { + regions <- match.arg(regions) + + map_data <- usmapdata::us_map(regions, data_year = data_year) + sf::st_geometry(map_data) <- NULL + map_data +} diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/us-map.R b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/us-map.R new file mode 100644 index 0000000..3f8ede1 --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/us-map.R @@ -0,0 +1,147 @@ +#' Retrieve US map data +#' +#' @param regions The region breakdown for the map, can be one of +#' (`"states"`, `"state"`, `"counties"`, `"county"`). +#' The default is `"states"`. +#' @param include The regions to include in the resulting map. If `regions` is +#' `"states"`/`"state"`, the value can be either a state name, abbreviation or FIPS code. +#' For counties, the FIPS must be provided as there can be multiple counties with the +#' same name. If states are provided in the county map, only counties in the included states +#' will be returned. +#' @param exclude The regions to exclude in the resulting map. If `regions` is +#' `"states"`/`"state"`, the value can be either a state name, abbreviation or FIPS code. +#' For counties, the FIPS must be provided as there can be multiple counties with the +#' same name. The regions listed in the `include` parameter take precedence over +#' regions listed in `exclude`. If both parameters include the same region(s) they +#' will be included in the map. +#' @param data_year The year for which to obtain map data. +#' If the value is `NULL`, the most recent year's data is used. If the +#' provided year is not found from the available map data sets, the next most +#' recent year's data is used. This can be used if an older data set is being +#' plotted on the US map so that the data matches the map more accurately. +#' Therefore, the provided value should match the year of the plotted data set. +#' The default is `NULL`, i.e. the most recent available year is used. +#' +#' @return An `sf` data frame of US map coordinates divided by the desired `regions`. +#' +#' @examples +#' str(us_map()) +#' +#' df <- us_map(regions = "counties") +#' west_coast <- us_map(include = c("CA", "OR", "WA")) +#' +#' excl_west_coast <- us_map(exclude = c("CA", "OR", "WA")) +#' +#' ct_counties_as_of_2022 <- us_map(regions = "counties", include = "CT", data_year = 2022) +#' +#' @export +us_map <- function( + regions = c("states", "state", "counties", "county"), + include = c(), + exclude = c(), + data_year = NULL +) { + regions <- match.arg(regions) + if (regions == "state") regions <- "states" + else if (regions == "county") regions <- "counties" + + map_year <- select_map_year(data_year) + file_name <- paste0("us_", regions, ".gpkg") + file_path <- system.file("extdata", map_year, file_name, package = "usmapdata") + df <- sf::read_sf(file_path, as_tibble = FALSE) + + # remove excluded items that are in `include` + exclude <- setdiff(exclude, include) + + # remove excludes + if (length(exclude) > 0) { + df <- df[!(df$full %in% exclude | + df$abbr %in% exclude | + df$fips %in% exclude | + substr(df$fips, 1, 2) %in% exclude), ] + } + + # remove non-includes + if (length(include) > 0) { + df <- df[df$full %in% include | + df$abbr %in% include | + df$fips %in% include | + substr(df$fips, 1, 2) %in% include, ] + } + + df[order(df$abbr), ] +} + +#' Retrieve centroid labels +#' +#' @inheritParams us_map +#' +#' @return An `sf` data frame of state or county centroid labels and positions +#' relative to the coordinates returned by the \link{us_map} function. +#' +#' @export +centroid_labels <- function( + regions = c("states", "state", "counties", "county"), + data_year = NULL +) { + regions <- match.arg(regions) + if (regions == "state") regions <- "states" + else if (regions == "county") regions <- "counties" + + map_year <- select_map_year(data_year) + file_name <- paste0("us_", regions, "_centroids.gpkg") + file_path <- system.file("extdata", map_year, file_name, package = "usmapdata") + + sf::read_sf(file_path, as_tibble = FALSE) +} + +#' Years for which US map data is available +#' +#' @return A numeric vector of available map data years, +#' sorted in descending order. +#' +#' @examples +#' available_map_years() +#' +#' @export +available_map_years <- function() { + sort(as.numeric(list.files(system.file("extdata", package = "usmapdata")))) +} + +#' Select appropriate map data year from available years +#' +#' @param data_year The year for which to obtain \link{us_map} data. +#' If the value is `NULL`, the most recent year is returned. If the +#' provided year is not found from the available map data sets, the next most +#' recent available year is returned. +#' +#' @keywords internal +select_map_year <- function(data_year) { + years <- available_map_years() + + if (is.null(data_year)) { + max(years) + } else if (!(data_year %in% years)) { + warn <- function(provided, used) { + warning( + paste0(provided, " map data not available, using ", used, " instead.\n\n", + "See available years with `usmapdata::available_map_years()`."), + call. = FALSE + ) + } + + years_greater <- years[years >= data_year] + + if (length(years_greater) == 0) { + last_available <- max(years[years < data_year]) + warn(data_year, last_available) + last_available + } else { + next_available <- min(years_greater) + warn(data_year, next_available) + next_available + } + } else { + data_year + } +} diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/usmapdata-package.R b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/usmapdata-package.R new file mode 100644 index 0000000..816860f --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/R/usmapdata-package.R @@ -0,0 +1,51 @@ +#' usmapdata: Mapping Data for usmap Package +#' +#' @description +#' It is usually difficult or inconvenient to create US maps that +#' include both Alaska and Hawaii in a convenient spot. All map +#' data frames produced by this package use the US National Atlas Equal Area +#' projection. +#' +#' @section Map data frames: +#' Alaska and Hawaii have been manually moved to a new location so that +#' their new coordinates place them to the bottom-left corner of +#' the map. These maps can be accessed by using the \link{us_map} function. +#' +#' The function provides the ability to retrieve maps with either +#' state borders or county borders using the `regions` parameter +#' for convenience. +#' +#' States (or counties) can be included such that all other states (or counties) +#' are excluded using the `include` parameter. +#' +#' @author Paolo Di Lorenzo \cr +#' \itemize{ +#' \item Email: \email{paolo@@dilorenzo.org} +#' \item GitHub: \url{https://github.com/pdil/} +#' } +#' +#' @seealso +#' Helpful links: +#' \itemize{ +#' \item US Census Shapefiles \cr +#' \url{https://www.census.gov/geographies/mapping-files/time-series/geo/tiger-line-file.html} +#' \item Map Features \cr +#' \url{https://en.wikipedia.org/wiki/Map_projection} +#' \url{https://en.wikipedia.org/wiki/Equal-area_projection} +#' \url{https://epsg.io/9311} +#' } +#' +#' @references { +#' Rudis B (2014). “Moving The Earth +#' (well, Alaska & Hawaii) With R.” +#' . +#' } +#' +#' @docType package +#' @name usmapdata +"_PACKAGE" + +## usethis namespace: start +#' @importFrom rlang .data +## usethis namespace: end +NULL diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/README.md b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/README.md new file mode 100644 index 0000000..b54bd5c --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/README.md @@ -0,0 +1,98 @@ +# 🗺 usmapdata + +[![CRAN](https://www.r-pkg.org/badges/version/usmapdata?color=blue)](https://cran.r-project.org/package=usmapdata) [![check](https://github.com/pdil/usmapdata/actions/workflows/check.yaml/badge.svg)](https://github.com/pdil/usmap/actions/workflows/check.yaml) [![codecov](https://codecov.io/gh/pdil/usmapdata/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pdil/usmapdata) + +You might be looking for the `usmap` package: [CRAN](https://cran.r-project.org/package=usmap) | [GitHub](https://github.com/pdil/usmap) | [Website](https://usmap.dev) + +## Purpose + +`usmapdata` is a container package for the map data frame used in the [`usmap`](https://github.com/pdil/usmap) package. This data has been extracted to keep `usmap` small and easier to maintain, while allowing the ability to keep the US map data frame updated as often as possible (independently of `usmap` updates). + +This package and repository will only contain functions and data relevant to the actual map and FIPS data used to draw the map in the `usmap` package. All other functions, including FIPS and mapping convenience functions, will be contained in the `usmap` [repository](https://github.com/pdil/usmap). + +## Map Data +The map data files that we use to plot the maps in R are located in the `inst/extdata` folder. They are generated from shapefiles published by the [US Census Bureau](https://www.census.gov/). Data files for maps and FIPS codes at both the state and county levels are included. + +The [Cartographic Boundary Files](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.html) are used for mapping in `usmap`—specifically the 1:20m scale files. This low resolution allows for small file sizes while still allowing enough detail for simple choropleths. The file description can be read [here](https://www.census.gov/programs-surveys/geography/technical-documentation/naming-convention/cartographic-boundary-file.html). + +Shapefiles are updated yearly by the US Census Bureau. This repository contains scripts which periodically check for new shapefiles and update the data in the package accordingly. For more details see the [`data-raw`](https://github.com/pdil/usmapdata/tree/master/data-raw) directory. + +## Installation +This package should only be installed if you intend to manipulate the US mapping data frame, which contains coordinates to draw the US state and county boundaries. If you're interested in plotting data on a US map, use the [`usmap`](https://github.com/pdil/usmap) package. + +📦 To install from CRAN (recommended), run the following code in an R console: +```r +install.packages("usmapdata") +``` + +### Developer Build +⚠️ The developer build may be unstable and not function correctly, use with caution. + +To install the package from this repository, run the following code in an R console: +```r +# install.package("devtools") +devtools::install_github("pdil/usmapdata") +``` +This method will provide the most recent developer build of `usmapdata`. + +## Usage +To begin using `usmapdata`, import the package using the `library` command: +```r +library(usmapdata) +``` + +## Additional Information + +### Coordinate System +`usmap` uses the [US National Atlas Equal Area](https://epsg.io/9311) coordinate system: + +
+ sf::st_crs(9311) + + ```r + #> Coordinate Reference System: + #> User input: EPSG:9311 + #> wkt: + #> PROJCRS["NAD27 / US National Atlas Equal Area", + #> BASEGEOGCRS["NAD27", + #> DATUM["North American Datum 1927", + #> ELLIPSOID["Clarke 1866",6378206.4,294.978698213898, + #> LENGTHUNIT["metre",1]]], + #> PRIMEM["Greenwich",0, + #> ANGLEUNIT["degree",0.0174532925199433]], + #> ID["EPSG",4267]], + #> CONVERSION["US National Atlas Equal Area", + #> METHOD["Lambert Azimuthal Equal Area (Spherical)", + #> ID["EPSG",1027]], + #> PARAMETER["Latitude of natural origin",45, + #> ANGLEUNIT["degree",0.0174532925199433], + #> ID["EPSG",8801]], + #> PARAMETER["Longitude of natural origin",-100, + #> ANGLEUNIT["degree",0.0174532925199433], + #> ID["EPSG",8802]], + #> PARAMETER["False easting",0, + #> LENGTHUNIT["metre",1], + #> ID["EPSG",8806]], + #> PARAMETER["False northing",0, + #> LENGTHUNIT["metre",1], + #> ID["EPSG",8807]]], + #> CS[Cartesian,2], + #> AXIS["easting (X)",east, + #> ORDER[1], + #> LENGTHUNIT["metre",1]], + #> AXIS["northing (Y)",north, + #> ORDER[2], + #> LENGTHUNIT["metre",1]], + #> USAGE[ + #> SCOPE["Statistical analysis."], + #> AREA["United States (USA) - onshore and offshore."], + #> BBOX[15.56,167.65,74.71,-65.69]], + #> ID["EPSG",9311]] + ``` +
+ +This [coordinate reference system (CRS)](https://www.nceas.ucsb.edu/sites/default/files/2020-04/OverviewCoordinateReferenceSystems.pdf) can also be obtained with `usmap::usmap_crs()`. + +## Acknowledgments +The code used to generate the map files was based on this blog post by [Bob Rudis](https://github.com/hrbrmstr): +[Moving The Earth (well, Alaska & Hawaii) With R](https://rud.is/b/2014/11/16/moving-the-earth-well-alaska-hawaii-with-r/) diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/WORDLIST b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/WORDLIST new file mode 100644 index 0000000..6735e66 --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/WORDLIST @@ -0,0 +1,11 @@ +GeoPackage +Gert +Rudis +affine +choropleths +codecov +csv +pre +tibble +usmap +️ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_counties.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_counties.gpkg new file mode 100644 index 0000000..47d6c32 Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_counties.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_counties_centroids.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_counties_centroids.gpkg new file mode 100644 index 0000000..92029ee Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_counties_centroids.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_states.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_states.gpkg new file mode 100644 index 0000000..2362fba Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_states.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_states_centroids.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_states_centroids.gpkg new file mode 100644 index 0000000..ecd08e4 Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2021/us_states_centroids.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_counties.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_counties.gpkg new file mode 100644 index 0000000..787b05e Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_counties.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_counties_centroids.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_counties_centroids.gpkg new file mode 100644 index 0000000..6c1a729 Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_counties_centroids.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_states.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_states.gpkg new file mode 100644 index 0000000..e2f408d Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_states.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_states_centroids.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_states_centroids.gpkg new file mode 100644 index 0000000..122ad1e Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2022/us_states_centroids.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_counties.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_counties.gpkg new file mode 100644 index 0000000..03f7d80 Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_counties.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_counties_centroids.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_counties_centroids.gpkg new file mode 100644 index 0000000..8c43b07 Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_counties_centroids.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_states.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_states.gpkg new file mode 100644 index 0000000..20f3e38 Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_states.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_states_centroids.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_states_centroids.gpkg new file mode 100644 index 0000000..a127a34 Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2023/us_states_centroids.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_counties.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_counties.gpkg new file mode 100644 index 0000000..77cc5e8 Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_counties.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_counties_centroids.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_counties_centroids.gpkg new file mode 100644 index 0000000..2371ddd Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_counties_centroids.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_states.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_states.gpkg new file mode 100644 index 0000000..da79274 Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_states.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_states_centroids.gpkg b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_states_centroids.gpkg new file mode 100644 index 0000000..43b1e38 Binary files /dev/null and b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/inst/extdata/2024/us_states_centroids.gpkg differ diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/available_map_years.Rd b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/available_map_years.Rd new file mode 100644 index 0000000..36466d7 --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/available_map_years.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/us-map.R +\name{available_map_years} +\alias{available_map_years} +\title{Years for which US map data is available} +\usage{ +available_map_years() +} +\value{ +A numeric vector of available map data years, +sorted in descending order. +} +\description{ +Years for which US map data is available +} +\examples{ +available_map_years() + +} diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/centroid_labels.Rd b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/centroid_labels.Rd new file mode 100644 index 0000000..8db3400 --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/centroid_labels.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/us-map.R +\name{centroid_labels} +\alias{centroid_labels} +\title{Retrieve centroid labels} +\usage{ +centroid_labels( + regions = c("states", "state", "counties", "county"), + data_year = NULL +) +} +\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"}.} + +\item{data_year}{The year for which to obtain map data. +If the value is \code{NULL}, the most recent year's data is used. If the +provided year is not found from the available map data sets, the next most +recent year's data is used. This can be used if an older data set is being +plotted on the US map so that the data matches the map more accurately. +Therefore, the provided value should match the year of the plotted data set. +The default is \code{NULL}, i.e. the most recent available year is used.} +} +\value{ +An \code{sf} data frame of state or county centroid labels and positions +relative to the coordinates returned by the \link{us_map} function. +} +\description{ +Retrieve centroid labels +} diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/create_us_map.Rd b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/create_us_map.Rd new file mode 100644 index 0000000..14343bb --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/create_us_map.Rd @@ -0,0 +1,89 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/create-us-map.R +\name{create_us_map} +\alias{create_us_map} +\alias{ea_crs} +\alias{transform2D} +\alias{transform_alaska} +\alias{transform_hawaii} +\alias{transform_puerto_rico} +\alias{compute_centroids} +\alias{alaska_bbox} +\alias{hawaii_bbox} +\alias{puerto_rico_bbox} +\title{Internal map creation tools} +\usage{ +create_us_map( + type = c("states", "counties"), + input_file, + output_dir, + output_file +) + +ea_crs() + +transform2D(angle = 0, scale = 1) + +transform_alaska(alaska) + +transform_hawaii(hawaii) + +transform_puerto_rico(puerto_rico) + +compute_centroids(polygons, iterations = 3, initial_width_step = 10) + +alaska_bbox() + +hawaii_bbox() + +puerto_rico_bbox() +} +\description{ +\code{create_us_map()} creates the modified shapefiles used by the +\link[usmap]{usmap} package. + +\code{ea_crs()} returns the US National Atlas Equal Area coordinate reference system +(CRS) used by this package and \link[usmap]{usmap}. + +\code{transform2D()} computes a two dimensional affine transformation matrix +for the provided rotation angle and scale factor. + +\code{transform_alaska()} applies the appropriate transform for the Alaska polygons. + +\code{transform_hawaii()} applies the appropriate transform for the Hawaii polygons. + +\code{transform_puerto_rico()} applies the appropriate transform for the Puerto Rico polygons. + +\code{compute_centroids()} computes the modified centroids for each state or +county polygon using a center-of-mass technique on the largest polygon in +the region. + +\code{alaska_bbox()} returns the bounding box of Alaska pre-transformation. + +\code{hawaii_bbox()} returns the bounding box of Hawaii pre-transformation. + +\code{puerto_rico_bbox()} returns the bounding box of Puerto Rico pre-transformation. +} +\note{ +Using these functions externally is not recommended since they make certain +undocumented assumptions that may not work with all inputs. + +It is strongly recommend that the \link[usmap]{usmap} package is used +directly. + +In some cases where the raw data is required, the \link{us_map} and +\link{centroid_labels} functions located in this package can be used instead. +} +\references{ +{ +Gert (2017). “How to calculate +polygon centroids in R (for +non-contiguous shapes).” +\url{https://gis.stackexchange.com/a/265475}. + +Rudis B (2014). “Moving The Earth +(well, Alaska & Hawaii) With R.” +\url{https://rud.is/b/2014/11/16/moving-the-earth-well-alaska-hawaii-with-r/}. +} +} +\keyword{internal} diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/fips_data.Rd b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/fips_data.Rd new file mode 100644 index 0000000..cd789ad --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/fips_data.Rd @@ -0,0 +1,37 @@ +% 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"), + data_year = NULL +) +} +\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"}.} + +\item{data_year}{The year for which to obtain map data. +If the value is \code{NULL}, the most recent year's data is used. If the +provided year is not found from the available map data sets, the next most +recent year's data is used. This can be used if an older data set is being +plotted on the US map so that the data matches the map more accurately. +Therefore, the provided value should match the year of the plotted data set. +The default is \code{NULL}, i.e. the most recent available year is used.} +} +\value{ +An 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/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/select_map_year.Rd b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/select_map_year.Rd new file mode 100644 index 0000000..716b576 --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/select_map_year.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/us-map.R +\name{select_map_year} +\alias{select_map_year} +\title{Select appropriate map data year from available years} +\usage{ +select_map_year(data_year) +} +\arguments{ +\item{data_year}{The year for which to obtain \link{us_map} data. +If the value is \code{NULL}, the most recent year is returned. If the +provided year is not found from the available map data sets, the next most +recent available year is returned.} +} +\description{ +Select appropriate map data year from available years +} +\keyword{internal} diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/us_map.Rd b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/us_map.Rd new file mode 100644 index 0000000..afc01f5 --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/us_map.Rd @@ -0,0 +1,56 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/us-map.R +\name{us_map} +\alias{us_map} +\title{Retrieve US map data} +\usage{ +us_map( + regions = c("states", "state", "counties", "county"), + include = c(), + exclude = c(), + data_year = NULL +) +} +\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"}.} + +\item{include}{The regions to include in the resulting map. If \code{regions} is +\code{"states"}/\code{"state"}, the value can be either a state name, abbreviation or FIPS code. +For counties, the FIPS must be provided as there can be multiple counties with the +same name. If states are provided in the county map, only counties in the included states +will be returned.} + +\item{exclude}{The regions to exclude in the resulting map. If \code{regions} is +\code{"states"}/\code{"state"}, the value can be either a state name, abbreviation or FIPS code. +For counties, the FIPS must be provided as there can be multiple counties with the +same name. The regions listed in the \code{include} parameter take precedence over +regions listed in \code{exclude}. If both parameters include the same region(s) they +will be included in the map.} + +\item{data_year}{The year for which to obtain map data. +If the value is \code{NULL}, the most recent year's data is used. If the +provided year is not found from the available map data sets, the next most +recent year's data is used. This can be used if an older data set is being +plotted on the US map so that the data matches the map more accurately. +Therefore, the provided value should match the year of the plotted data set. +The default is \code{NULL}, i.e. the most recent available year is used.} +} +\value{ +An \code{sf} data frame of US map coordinates divided by the desired \code{regions}. +} +\description{ +Retrieve US map data +} +\examples{ +str(us_map()) + +df <- us_map(regions = "counties") +west_coast <- us_map(include = c("CA", "OR", "WA")) + +excl_west_coast <- us_map(exclude = c("CA", "OR", "WA")) + +ct_counties_as_of_2022 <- us_map(regions = "counties", include = "CT", data_year = 2022) + +} diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/usmapdata.Rd b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/usmapdata.Rd new file mode 100644 index 0000000..13c3f0f --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/man/usmapdata.Rd @@ -0,0 +1,52 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/usmapdata-package.R +\docType{package} +\name{usmapdata} +\alias{usmapdata-package} +\alias{usmapdata} +\title{usmapdata: Mapping Data for usmap Package} +\description{ +It is usually difficult or inconvenient to create US maps that +include both Alaska and Hawaii in a convenient spot. All map +data frames produced by this package use the US National Atlas Equal Area +projection. +} +\section{Map data frames}{ + +Alaska and Hawaii have been manually moved to a new location so that +their new coordinates place them to the bottom-left corner of +the map. These maps can be accessed by using the \link{us_map} function. + +The function provides the ability to retrieve maps with either +state borders or county borders using the \code{regions} parameter +for convenience. + +States (or counties) can be included such that all other states (or counties) +are excluded using the \code{include} parameter. +} + +\references{ +{ +Rudis B (2014). “Moving The Earth +(well, Alaska & Hawaii) With R.” +\url{https://rud.is/b/2014/11/16/moving-the-earth-well-alaska-hawaii-with-r/}. +} +} +\seealso{ +Helpful links: +\itemize{ +\item US Census Shapefiles \cr +\url{https://www.census.gov/geographies/mapping-files/time-series/geo/tiger-line-file.html} +\item Map Features \cr +\url{https://en.wikipedia.org/wiki/Map_projection} +\url{https://en.wikipedia.org/wiki/Equal-area_projection} +\url{https://epsg.io/9311} +} +} +\author{ +Paolo Di Lorenzo \cr +\itemize{ +\item Email: \email{paolo@dilorenzo.org} +\item GitHub: \url{https://github.com/pdil/} +} +} diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/spelling.R b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/spelling.R new file mode 100644 index 0000000..1d74607 --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/spelling.R @@ -0,0 +1,6 @@ +if (requireNamespace("spelling", quietly = TRUE)) + spelling::spell_check_test( + vignettes = TRUE, + error = FALSE, + skip_on_cran = TRUE + ) diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/testthat.R b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/testthat.R new file mode 100644 index 0000000..41cb783 --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/testthat.R @@ -0,0 +1,12 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview +# * https://testthat.r-lib.org/articles/special-files.html + +library(testthat) +library(usmapdata) + +test_check("usmapdata") diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/testthat/test-fips-data.R b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/testthat/test-fips-data.R new file mode 100644 index 0000000..ad2372f --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/testthat/test-fips-data.R @@ -0,0 +1,41 @@ + +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]]), 52) + + expect_equal(fips[[1, "abbr"]], "AK") + expect_equal(fips[[1, "fips"]], "02") + expect_equal(fips[[1, "full"]], "Alaska") + + expect_equal(fips[[52, "abbr"]], "WY") + expect_equal(fips[[52, "fips"]], "56") + expect_equal(fips[[52, "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]]), 3222) + + expect_equal(county_fips[[1, "full"]], "Alaska") + expect_equal(county_fips[[1, "abbr"]], "AK") + expect_equal(county_fips[[1, "county"]], "Aleutians East Borough") + expect_equal(county_fips[[1, "fips"]], "02013") + + expect_equal(county_fips[[3222, "full"]], "Wyoming") + expect_equal(county_fips[[3222, "abbr"]], "WY") + expect_equal(county_fips[[3222, "county"]], "Weston County") + expect_equal(county_fips[[3222, "fips"]], "56045") +}) diff --git a/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/testthat/test-usmap.R b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/testthat/test-usmap.R new file mode 100644 index 0000000..b5e403e --- /dev/null +++ b/check/usmapdata.Rcheck/00_pkg_src/usmapdata/tests/testthat/test-usmap.R @@ -0,0 +1,111 @@ + +states_map <- us_map(regions = "states") +counties_map <- us_map(regions = "counties") + +test_that("structure of states df is correct", { + expect_equal(length(unique(states_map$fips)), 52) +}) + +test_that("correct states are included", { + include_abbr <- c("AK", "AL", "AZ") + map_abbr <- us_map(regions = "states", include = include_abbr) + + include_full <- c("Alaska", "Alabama", "Arizona") + map_full <- us_map(regions = "states", include = include_full) + + include_fips <- c("02", "01", "04") + map_fips <- us_map(regions = "states", include = include_fips) + + expect_equal(length(unique(map_abbr$fips)), length(include_abbr)) + expect_equal(length(unique(map_full$fips)), length(include_full)) + expect_equal(length(unique(map_fips$fips)), length(include_fips)) +}) + +test_that("correct states are excluded", { + full_map <- us_map(regions = "states") + + exclude_abbr <- c("AK", "AL", "AZ") + map_abbr <- us_map(regions = "states", exclude = exclude_abbr) + + exclude_full <- c("Alaska", "Alabama", "Arizona") + map_full <- us_map(regions = "states", exclude = exclude_full) + + exclude_fips <- c("02", "01", "04") + map_fips <- us_map(regions = "states", exclude = exclude_fips) + + expect_equal(length(unique(full_map$fips)) - length(unique(map_abbr$fips)), length(exclude_abbr)) + expect_equal(length(unique(full_map$fips)) - length(unique(map_full$fips)), length(exclude_full)) + expect_equal(length(unique(full_map$fips)) - length(unique(map_fips$fips)), length(exclude_fips)) +}) + +test_that("include takes precedence over exclude", { + full_map <- us_map(regions = "states") + + include <- c("AK", "AR", "CA") + exclude <- c("AK", "AL", "AZ") + map <- us_map(regions = "states", include = include, exclude = exclude) + + expect_in(map$abbr, include) + expect_true("AK" %in% map$abbr) + expect_false("AL" %in% map$abbr) + expect_false("AZ" %in% map$abbr) +}) + +test_that("structure of counties df is correct", { + expect_equal(length(unique(counties_map$fips)), 3222) +}) + +test_that("correct counties are included", { + include_fips <- c("34021", "34023", "34025") + map_fips <- us_map(regions = "counties", include = include_fips) + + expect_equal(length(unique(map_fips$fips)), length(include_fips)) +}) + +test_that("correct counties are excluded", { + exclude_fips <- c("34021", "34023", "34025") + map_fips <- us_map(regions = "counties", include = "NJ", exclude = exclude_fips) + map_nj <- us_map(regions = "counties", include = "NJ") + + expect_equal(length(unique(map_nj$fips)) - length(unique(map_fips$fips)), (length(exclude_fips))) +}) + +test_that("singular regions value returns same data frames as plural", { + state_map <- us_map(regions = "state") + county_map <- us_map(regions = "county") + + expect_identical(states_map, state_map) + expect_identical(counties_map, county_map) +}) + +test_that("error occurs for invalid region", { + expect_error(us_map(regions = "cities")) +}) + +test_that("centroid labels are loaded", { + expect_equal(length(centroid_labels("states")[[1]]), 52) + expect_equal(length(centroid_labels("counties")[[1]]), 3222) + expect_equal(length(centroid_labels("state")[[1]]), 52) + expect_equal(length(centroid_labels("county")[[1]]), 3222) + expect_identical(centroid_labels("counties"), centroid_labels("county")) + expect_identical(centroid_labels("states"), centroid_labels("state")) +}) + +test_that("warning produced for unavailable map years", { + expect_warning(us_map(data_year = 1900)) + expect_warning(us_map(data_year = 2100)) +}) + +test_that("correct year chosen when unavailable year provided", { + available_years <- usmapdata:::available_map_years() + + expect_equal(usmapdata:::select_map_year(2023), 2023) + expect_equal( + suppressWarnings(usmapdata:::select_map_year(1000)), + min(available_years) + ) + expect_equal( + suppressWarnings(usmapdata:::select_map_year(3000)), + max(available_years) + ) +}) diff --git a/check/usmapdata.Rcheck/00check.log b/check/usmapdata.Rcheck/00check.log new file mode 100644 index 0000000..df30b26 --- /dev/null +++ b/check/usmapdata.Rcheck/00check.log @@ -0,0 +1,65 @@ +* using log directory ‘/home/runner/work/usmapdata/usmapdata/check/usmapdata.Rcheck’ +* using R version 4.5.1 (2025-06-13) +* using platform: x86_64-pc-linux-gnu +* R was compiled by + gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0 + GNU Fortran (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0 +* running under: Ubuntu 24.04.2 LTS +* using session charset: UTF-8 +* using options ‘--no-manual --as-cran’ +* checking for file ‘usmapdata/DESCRIPTION’ ... OK +* this is package ‘usmapdata’ version ‘0.6.0.9000’ +* package encoding: UTF-8 +* checking package namespace information ... OK +* checking package dependencies ... OK +* checking if this is a source package ... OK +* checking if there is a namespace ... OK +* checking for executable files ... OK +* checking for hidden files and directories ... OK +* checking for portable file names ... OK +* checking for sufficient/correct file permissions ... OK +* checking whether package ‘usmapdata’ can be installed ... OK +* checking installed package size ... INFO + installed size is 10.0Mb + sub-directories of 1Mb or more: + extdata 9.9Mb +* checking package directory ... OK +* checking for future file timestamps ... OK +* checking DESCRIPTION meta-information ... OK +* checking top-level files ... OK +* checking for left-over files ... OK +* checking index information ... OK +* checking package subdirectories ... OK +* checking code files for non-ASCII characters ... OK +* checking R files for syntax errors ... OK +* checking whether the package can be loaded ... OK +* checking whether the package can be loaded with stated dependencies ... OK +* checking whether the package can be unloaded cleanly ... OK +* checking whether the namespace can be loaded with stated dependencies ... OK +* checking whether the namespace can be unloaded cleanly ... OK +* checking loading without being on the library search path ... OK +* checking dependencies in R code ... OK +* checking S3 generic/method consistency ... OK +* checking replacement functions ... OK +* checking foreign function calls ... OK +* checking R code for possible problems ... OK +* checking Rd files ... OK +* checking Rd metadata ... OK +* checking Rd line widths ... OK +* checking Rd cross-references ... INFO +Package unavailable to check Rd xrefs: ‘usmap’ +* checking for missing documentation entries ... OK +* checking for code/documentation mismatches ... OK +* checking Rd \usage sections ... OK +* checking Rd contents ... OK +* checking for unstated dependencies in examples ... OK +* checking examples ... OK +* checking for unstated dependencies in ‘tests’ ... OK +* checking tests ... OK + Running ‘spelling.R’ + Comparing ‘spelling.Rout’ to ‘spelling.Rout.save’ ... OK + Running ‘testthat.R’ +* checking for non-standard things in the check directory ... OK +* checking for detritus in the temp directory ... OK +* DONE +Status: OK diff --git a/check/usmapdata.Rcheck/00install.out b/check/usmapdata.Rcheck/00install.out new file mode 100644 index 0000000..4fe70c7 --- /dev/null +++ b/check/usmapdata.Rcheck/00install.out @@ -0,0 +1,13 @@ +* installing *source* package ‘usmapdata’ ... +** this is package ‘usmapdata’ version ‘0.6.0.9000’ +** using staged installation +** R +** inst +** byte-compile and prepare package for lazy loading +** help +*** installing help indices +** building package indices +** testing if installed package can be loaded from temporary location +** testing if installed package can be loaded from final location +** testing if installed package keeps a record of temporary installation path +* DONE (usmapdata) diff --git a/check/usmapdata.Rcheck/R_check_bin/R b/check/usmapdata.Rcheck/R_check_bin/R new file mode 100755 index 0000000..3e289bc --- /dev/null +++ b/check/usmapdata.Rcheck/R_check_bin/R @@ -0,0 +1,2 @@ +echo "'R' should not be used without a path -- see par. 1.6 of the manual" +exit 1 diff --git a/check/usmapdata.Rcheck/R_check_bin/Rscript b/check/usmapdata.Rcheck/R_check_bin/Rscript new file mode 100755 index 0000000..6fead74 --- /dev/null +++ b/check/usmapdata.Rcheck/R_check_bin/Rscript @@ -0,0 +1,2 @@ +echo "'Rscript' should not be used without a path -- see par. 1.6 of the manual" +exit 1 diff --git a/check/usmapdata.Rcheck/tests/spelling.R b/check/usmapdata.Rcheck/tests/spelling.R new file mode 100644 index 0000000..1d74607 --- /dev/null +++ b/check/usmapdata.Rcheck/tests/spelling.R @@ -0,0 +1,6 @@ +if (requireNamespace("spelling", quietly = TRUE)) + spelling::spell_check_test( + vignettes = TRUE, + error = FALSE, + skip_on_cran = TRUE + ) diff --git a/check/usmapdata.Rcheck/tests/spelling.Rout b/check/usmapdata.Rcheck/tests/spelling.Rout new file mode 100644 index 0000000..904d0ac --- /dev/null +++ b/check/usmapdata.Rcheck/tests/spelling.Rout @@ -0,0 +1,28 @@ + +R version 4.5.1 (2025-06-13) -- "Great Square Root" +Copyright (C) 2025 The R Foundation for Statistical Computing +Platform: x86_64-pc-linux-gnu + +R is free software and comes with ABSOLUTELY NO WARRANTY. +You are welcome to redistribute it under certain conditions. +Type 'license()' or 'licence()' for distribution details. + +R is a collaborative project with many contributors. +Type 'contributors()' for more information and +'citation()' on how to cite R or R packages in publications. + +Type 'demo()' for some demos, 'help()' for on-line help, or +'help.start()' for an HTML browser interface to help. +Type 'q()' to quit R. + +> if (requireNamespace("spelling", quietly = TRUE)) ++ spelling::spell_check_test( ++ vignettes = TRUE, ++ error = FALSE, ++ skip_on_cran = TRUE ++ ) +All Done! +> +> proc.time() + user system elapsed + 0.465 0.353 0.476 diff --git a/check/usmapdata.Rcheck/tests/spelling.Rout.save b/check/usmapdata.Rcheck/tests/spelling.Rout.save new file mode 100644 index 0000000..16a5e3a --- /dev/null +++ b/check/usmapdata.Rcheck/tests/spelling.Rout.save @@ -0,0 +1,28 @@ + +R version 3.4.1 (2017-06-30) -- "Single Candle" +Copyright (C) 2017 The R Foundation for Statistical Computing +Platform: x86_64-apple-darwin15.6.0 (64-bit) + +R is free software and comes with ABSOLUTELY NO WARRANTY. +You are welcome to redistribute it under certain conditions. +Type 'license()' or 'licence()' for distribution details. + +R is a collaborative project with many contributors. +Type 'contributors()' for more information and +'citation()' on how to cite R or R packages in publications. + +Type 'demo()' for some demos, 'help()' for on-line help, or +'help.start()' for an HTML browser interface to help. +Type 'q()' to quit R. + +> if (requireNamespace("spelling", quietly = TRUE)) ++ spelling::spell_check_test( ++ vignettes = TRUE, ++ error = FALSE, ++ skip_on_cran = TRUE ++ ) +All Done! +> +> proc.time() + user system elapsed + 0.372 0.039 0.408 diff --git a/check/usmapdata.Rcheck/tests/startup.Rs b/check/usmapdata.Rcheck/tests/startup.Rs new file mode 100644 index 0000000..8ad6d25 --- /dev/null +++ b/check/usmapdata.Rcheck/tests/startup.Rs @@ -0,0 +1,4 @@ +## A custom startup file for tests +## Run as if a system Rprofile, so no packages, no assignments +options(useFancyQuotes = FALSE) + diff --git a/check/usmapdata.Rcheck/tests/testthat.R b/check/usmapdata.Rcheck/tests/testthat.R new file mode 100644 index 0000000..41cb783 --- /dev/null +++ b/check/usmapdata.Rcheck/tests/testthat.R @@ -0,0 +1,12 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview +# * https://testthat.r-lib.org/articles/special-files.html + +library(testthat) +library(usmapdata) + +test_check("usmapdata") diff --git a/check/usmapdata.Rcheck/tests/testthat.Rout b/check/usmapdata.Rcheck/tests/testthat.Rout new file mode 100644 index 0000000..188d92b --- /dev/null +++ b/check/usmapdata.Rcheck/tests/testthat.Rout @@ -0,0 +1,34 @@ + +R version 4.5.1 (2025-06-13) -- "Great Square Root" +Copyright (C) 2025 The R Foundation for Statistical Computing +Platform: x86_64-pc-linux-gnu + +R is free software and comes with ABSOLUTELY NO WARRANTY. +You are welcome to redistribute it under certain conditions. +Type 'license()' or 'licence()' for distribution details. + +R is a collaborative project with many contributors. +Type 'contributors()' for more information and +'citation()' on how to cite R or R packages in publications. + +Type 'demo()' for some demos, 'help()' for on-line help, or +'help.start()' for an HTML browser interface to help. +Type 'q()' to quit R. + +> # This file is part of the standard setup for testthat. +> # It is recommended that you do not modify it. +> # +> # Where should you do additional test configuration? +> # Learn more about the roles of various files in: +> # * https://r-pkgs.org/testing-design.html#sec-tests-files-overview +> # * https://testthat.r-lib.org/articles/special-files.html +> +> library(testthat) +> library(usmapdata) +> +> test_check("usmapdata") +[ FAIL 0 | WARN 0 | SKIP 0 | PASS 50 ] +> +> proc.time() + user system elapsed + 1.294 0.391 1.344 diff --git a/check/usmapdata.Rcheck/tests/testthat/test-fips-data.R b/check/usmapdata.Rcheck/tests/testthat/test-fips-data.R new file mode 100644 index 0000000..ad2372f --- /dev/null +++ b/check/usmapdata.Rcheck/tests/testthat/test-fips-data.R @@ -0,0 +1,41 @@ + +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]]), 52) + + expect_equal(fips[[1, "abbr"]], "AK") + expect_equal(fips[[1, "fips"]], "02") + expect_equal(fips[[1, "full"]], "Alaska") + + expect_equal(fips[[52, "abbr"]], "WY") + expect_equal(fips[[52, "fips"]], "56") + expect_equal(fips[[52, "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]]), 3222) + + expect_equal(county_fips[[1, "full"]], "Alaska") + expect_equal(county_fips[[1, "abbr"]], "AK") + expect_equal(county_fips[[1, "county"]], "Aleutians East Borough") + expect_equal(county_fips[[1, "fips"]], "02013") + + expect_equal(county_fips[[3222, "full"]], "Wyoming") + expect_equal(county_fips[[3222, "abbr"]], "WY") + expect_equal(county_fips[[3222, "county"]], "Weston County") + expect_equal(county_fips[[3222, "fips"]], "56045") +}) diff --git a/check/usmapdata.Rcheck/tests/testthat/test-usmap.R b/check/usmapdata.Rcheck/tests/testthat/test-usmap.R new file mode 100644 index 0000000..b5e403e --- /dev/null +++ b/check/usmapdata.Rcheck/tests/testthat/test-usmap.R @@ -0,0 +1,111 @@ + +states_map <- us_map(regions = "states") +counties_map <- us_map(regions = "counties") + +test_that("structure of states df is correct", { + expect_equal(length(unique(states_map$fips)), 52) +}) + +test_that("correct states are included", { + include_abbr <- c("AK", "AL", "AZ") + map_abbr <- us_map(regions = "states", include = include_abbr) + + include_full <- c("Alaska", "Alabama", "Arizona") + map_full <- us_map(regions = "states", include = include_full) + + include_fips <- c("02", "01", "04") + map_fips <- us_map(regions = "states", include = include_fips) + + expect_equal(length(unique(map_abbr$fips)), length(include_abbr)) + expect_equal(length(unique(map_full$fips)), length(include_full)) + expect_equal(length(unique(map_fips$fips)), length(include_fips)) +}) + +test_that("correct states are excluded", { + full_map <- us_map(regions = "states") + + exclude_abbr <- c("AK", "AL", "AZ") + map_abbr <- us_map(regions = "states", exclude = exclude_abbr) + + exclude_full <- c("Alaska", "Alabama", "Arizona") + map_full <- us_map(regions = "states", exclude = exclude_full) + + exclude_fips <- c("02", "01", "04") + map_fips <- us_map(regions = "states", exclude = exclude_fips) + + expect_equal(length(unique(full_map$fips)) - length(unique(map_abbr$fips)), length(exclude_abbr)) + expect_equal(length(unique(full_map$fips)) - length(unique(map_full$fips)), length(exclude_full)) + expect_equal(length(unique(full_map$fips)) - length(unique(map_fips$fips)), length(exclude_fips)) +}) + +test_that("include takes precedence over exclude", { + full_map <- us_map(regions = "states") + + include <- c("AK", "AR", "CA") + exclude <- c("AK", "AL", "AZ") + map <- us_map(regions = "states", include = include, exclude = exclude) + + expect_in(map$abbr, include) + expect_true("AK" %in% map$abbr) + expect_false("AL" %in% map$abbr) + expect_false("AZ" %in% map$abbr) +}) + +test_that("structure of counties df is correct", { + expect_equal(length(unique(counties_map$fips)), 3222) +}) + +test_that("correct counties are included", { + include_fips <- c("34021", "34023", "34025") + map_fips <- us_map(regions = "counties", include = include_fips) + + expect_equal(length(unique(map_fips$fips)), length(include_fips)) +}) + +test_that("correct counties are excluded", { + exclude_fips <- c("34021", "34023", "34025") + map_fips <- us_map(regions = "counties", include = "NJ", exclude = exclude_fips) + map_nj <- us_map(regions = "counties", include = "NJ") + + expect_equal(length(unique(map_nj$fips)) - length(unique(map_fips$fips)), (length(exclude_fips))) +}) + +test_that("singular regions value returns same data frames as plural", { + state_map <- us_map(regions = "state") + county_map <- us_map(regions = "county") + + expect_identical(states_map, state_map) + expect_identical(counties_map, county_map) +}) + +test_that("error occurs for invalid region", { + expect_error(us_map(regions = "cities")) +}) + +test_that("centroid labels are loaded", { + expect_equal(length(centroid_labels("states")[[1]]), 52) + expect_equal(length(centroid_labels("counties")[[1]]), 3222) + expect_equal(length(centroid_labels("state")[[1]]), 52) + expect_equal(length(centroid_labels("county")[[1]]), 3222) + expect_identical(centroid_labels("counties"), centroid_labels("county")) + expect_identical(centroid_labels("states"), centroid_labels("state")) +}) + +test_that("warning produced for unavailable map years", { + expect_warning(us_map(data_year = 1900)) + expect_warning(us_map(data_year = 2100)) +}) + +test_that("correct year chosen when unavailable year provided", { + available_years <- usmapdata:::available_map_years() + + expect_equal(usmapdata:::select_map_year(2023), 2023) + expect_equal( + suppressWarnings(usmapdata:::select_map_year(1000)), + min(available_years) + ) + expect_equal( + suppressWarnings(usmapdata:::select_map_year(3000)), + max(available_years) + ) +}) diff --git a/check/usmapdata.Rcheck/usmapdata-Ex.R b/check/usmapdata.Rcheck/usmapdata-Ex.R new file mode 100644 index 0000000..518b8d0 --- /dev/null +++ b/check/usmapdata.Rcheck/usmapdata-Ex.R @@ -0,0 +1,101 @@ +pkgname <- "usmapdata" +source(file.path(R.home("share"), "R", "examples-header.R")) +options(warn = 1) +base::assign(".ExTimings", "usmapdata-Ex.timings", pos = 'CheckExEnv') +base::cat("name\tuser\tsystem\telapsed\n", file=base::get(".ExTimings", pos = 'CheckExEnv')) +base::assign(".format_ptime", +function(x) { + if(!is.na(x[4L])) x[1L] <- x[1L] + x[4L] + if(!is.na(x[5L])) x[2L] <- x[2L] + x[5L] + options(OutDec = '.') + format(x[1L:3L], digits = 7L) +}, +pos = 'CheckExEnv') + +### * +library('usmapdata') + +base::assign(".oldSearch", base::search(), pos = 'CheckExEnv') +base::assign(".old_wd", base::getwd(), pos = 'CheckExEnv') +cleanEx() +nameEx("available_map_years") +### * available_map_years + +flush(stderr()); flush(stdout()) + +base::assign(".ptime", proc.time(), pos = "CheckExEnv") +### Name: available_map_years +### Title: Years for which US map data is available +### Aliases: available_map_years + +### ** Examples + +available_map_years() + + + + +base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +base::cat("available_map_years", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +cleanEx() +nameEx("fips_data") +### * fips_data + +flush(stderr()); flush(stdout()) + +base::assign(".ptime", proc.time(), pos = "CheckExEnv") +### Name: fips_data +### Title: Retrieve state and county FIPS codes +### Aliases: fips_data + +### ** Examples + +str(fips_data()) + +state_fips <- fips_data() +county_fips <- fips_data(regions = "counties") + + + + +base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +base::cat("fips_data", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +cleanEx() +nameEx("us_map") +### * us_map + +flush(stderr()); flush(stdout()) + +base::assign(".ptime", proc.time(), pos = "CheckExEnv") +### Name: us_map +### Title: Retrieve US map data +### Aliases: us_map + +### ** Examples + +str(us_map()) + +df <- us_map(regions = "counties") +west_coast <- us_map(include = c("CA", "OR", "WA")) + +excl_west_coast <- us_map(exclude = c("CA", "OR", "WA")) + +ct_counties_as_of_2022 <- us_map(regions = "counties", include = "CT", data_year = 2022) + + + + +base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +base::cat("us_map", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +### *