Skip to content

Commit

Permalink
Avoid using .onLoad to make package data available
Browse files Browse the repository at this point in the history
Add prefixing where needed

Also fix minor issues w/ cli_abort_ifnot and cli_warn_ifnot
  • Loading branch information
elipousson committed Oct 9, 2024
1 parent e640f16 commit ef402ff
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 24 deletions.
3 changes: 2 additions & 1 deletion R/convert_dist_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ set_dist_units <- function(x = NULL,
value <-
arg_match(
value,
c(dist_unit_options, area_unit_options),
c(sfext::dist_unit_options,
sfext::area_unit_options),
error_call = call
)

Expand Down
5 changes: 4 additions & 1 deletion R/get_paper.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,15 @@ set_paper_orientation <- function(paper, orientation = NULL, bbox = NULL) {
#'
#' @noRd
get_paper_name <- function(paper) {
paper_sizes <- sfext::paper_sizes
paper_sizes[tolower(paper_sizes[["name"]]) %in% tolower(paper), ]
}

#' Get paper using standard and optional series or size
#'
#' @noRd
get_paper_standard <- function(standard, series = NULL, size = NULL) {
paper_sizes <- sfext::paper_sizes
standard <- match.arg(standard, c("ANSI", "ISO", "British Imperial", "JIS", "USPS", "Facebook", "Instagram", "Twitter"), several.ok = TRUE)
paper <- paper_sizes[paper_sizes[["standard"]] %in% standard, ]

Expand All @@ -184,6 +186,7 @@ get_paper_standard <- function(standard, series = NULL, size = NULL) {
#' @noRd
#' @importFrom dplyr filter
get_paper_dims <- function(width = NULL, height = NULL, units = NULL) {
paper_sizes <- sfext::paper_sizes
units <- match.arg(tolower(units), c("in", "mm", "px"))
paper <- paper_sizes[paper_sizes[["units"]] %in% units, ]

Expand Down Expand Up @@ -212,7 +215,7 @@ get_paper_dims <- function(width = NULL, height = NULL, units = NULL) {
#' @export
get_social_image <- function(image = NULL, platform = NULL, format = NULL, orientation = NULL) {
lifecycle::signal_stage("superseded", "sfext::get_social_image()", "papersize::get_social_size()")

paper_sizes <- sfext::paper_sizes
image_sizes <- paper_sizes[paper_sizes$type == "social", ]

if (!is_null(platform)) {
Expand Down
5 changes: 3 additions & 2 deletions R/get_scale.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
get_scale <- function(scale = NULL,
standard = NULL,
series = NULL) {
select_scale <- standard_scales
select_scale <- sfext::standard_scales

if (!is_null(scale)) {
select_scale <- dplyr::filter(select_scale, .data$scale %in% {{ scale }})
Expand All @@ -29,7 +29,8 @@ get_scale <- function(scale = NULL,
}

if (!is_null(series)) {
series <- arg_match(series, unique(standard_scales$series), multiple = TRUE)
series_values <- unique(sfext::standard_scales$series)
series <- arg_match(series, series_values, multiple = TRUE)
select_scale <- dplyr::filter(select_scale, .data$series %in% {{ series }})
}

Expand Down
11 changes: 6 additions & 5 deletions R/is_dist_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#' @family dist
#' @export
is_dist_units <- function(x) {
is_units(x) && (get_dist_units(x) %in% c(dist_unit_options, area_unit_options))
is_units(x) && (get_dist_units(x) %in% c(sfext::dist_unit_options, sfext::area_unit_options))
}

#' @name diff_dist
Expand Down Expand Up @@ -124,18 +124,19 @@ get_dist_units <- function(x, allow_null = TRUE, multiple = TRUE, quiet = FALSE)
return(
arg_match(
x,
c(dist_unit_options, area_unit_options),
c(sfext::dist_unit_options,
sfext::area_unit_options),
multiple = multiple
)
)
}

if (is_units(x)) {
x_is_dist_unit <-
all(as.character(units(x)[["numerator"]]) %in% dist_unit_options)
all(as.character(units(x)[["numerator"]]) %in% sfext::dist_unit_options)

x_not_area_unit <-
!(as.character(units(x)) %in% area_unit_options)
!(as.character(units(x)) %in% sfext::area_unit_options)

if (x_is_dist_unit && x_not_area_unit) {
return(as.character(units(x)[["numerator"]]))
Expand Down Expand Up @@ -185,7 +186,7 @@ as_dist_units <- function(x,
units <-
arg_match(
units,
c(dist_unit_options, area_unit_options),
c(sfext::dist_unit_options, sfext::area_unit_options),
error_call = call
)

Expand Down
2 changes: 1 addition & 1 deletion R/sf_bbox_dist.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ sf_bbox_dist <- function(bbox,
units <-
arg_match(
units,
c(units_gdal, dist_unit_options)
c(units_gdal, sfext::dist_unit_options)
)

dist <-
Expand Down
20 changes: 6 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
# has_fileext int_to_alpha as_numbered_labels str_add_fileext
# str_remove_fileext str_extract_fileext has_min_length

.onLoad <- function(lib, pkg) {
utils::data(
list = c(
"dist_units",
"dist_unit_options", "area_unit_options",
"standard_scales", "paper_sizes"
),
package = pkg,
envir = parent.env(environment())
)
}

utils::globalVariables(
c(
"filename", "image_description", "image_height", "image_width", "latitude",
Expand All @@ -33,12 +21,14 @@ utils::globalVariables(
)

#' @noRd
cli_abort_ifnot <- function(...,
cli_abort_ifnot <- function(x = NULL,
...,
message = NULL,
arg = caller_arg(x),
.envir = call,
call = caller_env()) {
cli_ifnot(
x = x,
...,
message = message,
.default = cli::cli_abort,
Expand All @@ -50,12 +40,14 @@ cli_abort_ifnot <- function(...,


#' @noRd
cli_warn_ifnot <- function(...,
cli_warn_ifnot <- function(x = NULL,
...,
message = NULL,
arg = caller_arg(x),
.envir = call,
call = caller_env()) {
cli_ifnot(
x = x,
...,
message = message,
.default = cli::cli_warn,
Expand Down

0 comments on commit ef402ff

Please sign in to comment.