Skip to content

Commit

Permalink
Move sp set_evolution_status function and update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pdil committed Jun 8, 2023
1 parent dcf44c8 commit d0a273d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ Suggests:
sf,
stringr,
testthat
RoxygenNote: 7.2.2
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
13 changes: 8 additions & 5 deletions R/plot-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,18 @@ plot_usmap <- function(regions = c("states", "state", "counties", "county"),
ggplot2::ggplot(data = map_df) + polygon_layer + label_layer + ggplot2::coord_equal() + theme
}

#' This creates a nice map theme for use in plot_usmap.
#' It is borrowed from the ggthemes package located at this repository:
#' https://github.com/jrnold/ggthemes
#' Convenient theme map
#'
#' @description
#' This creates a nice map theme for use in [plot_usmap].
#' It is borrowed from the `ggthemes` package located at this repository:
#' https://github.com/jrnold/ggthemes.
#'
#' This function was manually rewritten here to avoid the need for
#' another package import.
#'
#' All theme functions (i.e. theme_bw, theme, element_blank, %+replace%)
#' come from ggplot2.
#' All theme functions (i.e. `theme_bw`, `theme`, `element_blank`, `%+replace%`)
#' come from [ggplot2].
#'
#' @keywords internal
theme_map <- function(base_size = 9, base_family = "") {
Expand Down
19 changes: 19 additions & 0 deletions R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ usmap_transform <- function(data,
output_names = c("x", "y")) {

# check for sf
set_sp_evolution_status()
if (!requireNamespace("sf", quietly = TRUE)) {
stop("`sf` must be installed to use `usmap_transform`.
Use: install.packages(\"sf\") and try again.")
Expand Down Expand Up @@ -199,6 +200,8 @@ usmap_transform.data.frame <- function(data,
#'
#' @export
usmap_crs <- function() {
set_sp_evolution_status()

if (!requireNamespace("sf", quietly = TRUE)) {
stop("`sf` must be installed to use `usmap_transform`.
Use: install.packages(\"sf\") and try again.")
Expand All @@ -212,3 +215,19 @@ usmap_crs <- function() {
sp::CRS(paste("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0",
"+a=6370997 +b=6370997 +units=m +no_defs"))
}

#' Set sp evolution status
#'
#' @description
#' Sets the `sp` evolution status to "2" to
#' force usage of `sf` instead of `rgdal`
#' which is being retired.
#'
#' This can be removed in the future when the evolution status
#' is set to >= 2 by default in `sf`.
#'
#' @keywords internal
set_sp_evolution_status <- function() {
if (sp::get_evolution_status() < 2L)
sp::set_evolution_status(2L)
}
7 changes: 0 additions & 7 deletions R/usmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@
if (getRversion() >= "2.15.1")
utils::globalVariables(c("abbr", "county", "group", "x", "y"))

.onLoad <- function(libname, pkgname) {
## Ensure latest evolution status of sp is used.
## Replaces rgdal usage with sf package.
if (sp::get_evolution_status() != 2L)
sp::set_evolution_status(2L)
}

#' Retrieve US map data
#'
#' @param regions The region breakdown for the map, can be one of
Expand Down
17 changes: 17 additions & 0 deletions man/set_sp_evolution_status.Rd

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

15 changes: 8 additions & 7 deletions man/theme_map.Rd

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

0 comments on commit d0a273d

Please sign in to comment.