diff --git a/R/fips.R b/R/fips.R index a33e38e..7f9e86f 100644 --- a/R/fips.R +++ b/R/fips.R @@ -86,9 +86,8 @@ fips <- function(state, county = c()) { result <- c( result, df$fips[which( - (name %in% county_i | name %in% paste(county_i, "county")) - & - (state_abbr %in% state_ | state_full %in% state_) + (name %in% county_i | name %in% paste(county_i, "county")) & + (state_abbr %in% state_ | state_full %in% state_) )] ) } diff --git a/R/plot-map.R b/R/plot-map.R index 3b16e24..1e91b9b 100644 --- a/R/plot-map.R +++ b/R/plot-map.R @@ -123,7 +123,8 @@ plot_usmap <- function(regions = c("states", "state", "counties", "county"), centroid_labels <- centroid_labels[ centroid_labels$full %in% include | centroid_labels$abbr %in% include | - centroid_labels$fips %in% include, ] + centroid_labels$fips %in% include, + ] } if (length(exclude) > 0) { @@ -131,18 +132,21 @@ plot_usmap <- function(regions = c("states", "state", "counties", "county"), centroid_labels$full %in% exclude | centroid_labels$abbr %in% exclude | centroid_labels$fips %in% exclude | - substr(centroid_labels$fips, 1, 2) %in% exclude), ] + substr(centroid_labels$fips, 1, 2) %in% exclude + ), ] } if (regions_ == "county" || regions_ == "counties") { label_layer <- ggplot2::geom_text( data = centroid_labels, ggplot2::aes(x = .data$x, y = .data$y, label = sub(" County", "", .data$county)), - color = label_color) + color = label_color + ) } else { label_layer <- ggplot2::geom_text( data = centroid_labels, - ggplot2::aes(x = .data$x, y = .data$y, label = .data$abbr), color = label_color) + ggplot2::aes(x = .data$x, y = .data$y, label = .data$abbr), color = label_color + ) } } else { label_layer <- ggplot2::geom_blank() diff --git a/R/transform.R b/R/transform.R index addf21b..c34f573 100644 --- a/R/transform.R +++ b/R/transform.R @@ -153,9 +153,9 @@ usmap_transform.data.frame <- function(data, hawaii <- transformed[ transformed@coords[, 1] >= hi_bbox[1, 1] & - transformed@coords[, 1] <= hi_bbox[1, 2] & - transformed@coords[, 2] >= hi_bbox[2, 1] & - transformed@coords[, 2] <= hi_bbox[2, 2], + transformed@coords[, 1] <= hi_bbox[1, 2] & + transformed@coords[, 2] >= hi_bbox[2, 1] & + transformed@coords[, 2] <= hi_bbox[2, 2], ] if (length(hawaii) > 0) { diff --git a/tests/testthat/helper-requireNamespace-mock.R b/tests/testthat/helper-requireNamespace-mock.R new file mode 100644 index 0000000..ec0d4bf --- /dev/null +++ b/tests/testthat/helper-requireNamespace-mock.R @@ -0,0 +1,12 @@ + +# Mocks `requireNamespace` to return `FALSE` for +# provided dependency package "pkg" to test that +# error occurs when it's not installed. +expect_package_error <- function(pkg, code, msg) { + testthat::with_mocked_bindings({ + testthat::expect_error(code, paste0("`", pkg, "` must be installed")) + }, + requireNamespace = function(package, ...) package != pkg, + .package = "base" + ) +} diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index 68a4a6f..7946bd8 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -1,5 +1,9 @@ context("Plotting US maps") +test_that("dependencies are verified", { + expect_package_error("ggplot2", plot_usmap()) +}) + library(proto) example_data <- data.frame( diff --git a/tests/testthat/test-transform.R b/tests/testthat/test-transform.R index 7b2b6a0..a27e2f4 100644 --- a/tests/testthat/test-transform.R +++ b/tests/testthat/test-transform.R @@ -1,5 +1,12 @@ context("Transforming coordinate data frames") +test_that("dependencies are verified", { + expect_package_error("sf", usmap_transform(data.frame())) + expect_package_error("sp", usmap_transform(data.frame())) + expect_package_error("sf", usmap_crs()) + expect_package_error("sp", usmap_crs()) +}) + test_that("data frame with AK and HI points is transformed", { data <- data.frame( lon = c(-74.01, -95.36, -118.24, -87.65, -134.42, -157.86), @@ -104,3 +111,9 @@ test_that("error occurs for data with non-numeric columns", { expect_error(usmap_transform(invalid_data2)) expect_error(usmap_transform(invalid_data3)) }) + +test_that("sp evolution status is set correctly", { + sp::set_evolution_status(1L) + usmap_transform(data.frame(lon = 0, lat = 0)) + expect_equal(sp::get_evolution_status(), 2L) +}) diff --git a/tests/testthat/test-usmap.R b/tests/testthat/test-usmap.R new file mode 100644 index 0000000..5a8cb32 --- /dev/null +++ b/tests/testthat/test-usmap.R @@ -0,0 +1,11 @@ +context("Retrieving US map data") + +test_that("dependencies are verified", { + expect_package_error("usmapdata", us_map()) +}) + +test_that("data frame is returned", { + data <- us_map() + expect_equal(length(data), 9) + expect_equal(length(data$x), 13663) +})