Skip to content

Use rlang::check_installed() #4375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Imports:
isoband,
MASS,
mgcv,
rlang (>= 0.3.0),
rlang (>= 0.4.10),
scales (>= 0.5.0),
stats,
tibble,
Expand Down
1 change: 1 addition & 0 deletions R/coord-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ CoordMap <- ggproto("CoordMap", Coord,


mproject <- function(coord, x, y, orientation) {
check_installed("mapproj", reason = "for `coord_map()`")
suppressWarnings(mapproj::mapproject(x, y,
projection = coord$projection,
parameters = coord$params,
Expand Down
2 changes: 1 addition & 1 deletion R/fortify-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fortify.map <- function(model, data, ...) {
#' coord_map("albers", lat0 = 45.5, lat1 = 29.5)
#' }
map_data <- function(map, region = ".", exact = FALSE, ...) {
try_require("maps", "map_data")
check_installed("maps", reason = "for `map_data()`")
map_obj <- maps::map(map, region, exact = exact, plot = FALSE, fill = TRUE, ...)
fortify(map_obj)
}
Expand Down
8 changes: 2 additions & 6 deletions R/fortify.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ fortify.data.frame <- function(model, data, ...) model
fortify.tbl_df <- function(model, data, ...) model
#' @export
fortify.tbl <- function(model, data, ...) {
if (!requireNamespace("dplyr", quietly = TRUE)) {
abort("dplyr must be installed to work with tbl objects")
}
check_installed("dplyr", reason = "to work with `tbl` objects")
dplyr::collect(model)
}
#' @export
Expand All @@ -31,9 +29,7 @@ fortify.function <- function(model, data, ...) model
fortify.formula <- function(model, data, ...) as_function(model)
#' @export
fortify.grouped_df <- function(model, data, ...) {
if (!requireNamespace("dplyr", quietly = TRUE)) {
abort("dplyr must be installed to work with grouped_df objects")
}
check_installed("dplyr", reason = "to work with `grouped_df` objects")
model$.group <- dplyr::group_indices(model)
model
}
Expand Down
2 changes: 1 addition & 1 deletion R/stat-binhex.r
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ StatBinhex <- ggproto("StatBinhex", Stat,

compute_group = function(data, scales, binwidth = NULL, bins = 30,
na.rm = FALSE) {
try_require("hexbin", "stat_binhex")
check_installed("hexbin", reason = "for `stat_binhex()`")

binwidth <- binwidth %||% hex_binwidth(bins, scales)
wt <- data$weight %||% rep(1L, nrow(data))
Expand Down
2 changes: 1 addition & 1 deletion R/stat-quantile.r
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ StatQuantile <- ggproto("StatQuantile", Stat,
compute_group = function(data, scales, quantiles = c(0.25, 0.5, 0.75),
formula = NULL, xseq = NULL, method = "rq",
method.args = list(), lambda = 1, na.rm = FALSE) {
try_require("quantreg", "stat_quantile")
check_installed("quantreg", reason = "for `stat_quantile()`")

if (is.null(formula)) {
if (method == "rqss") {
Expand Down
2 changes: 1 addition & 1 deletion R/stat-summary-hex.r
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ StatSummaryHex <- ggproto("StatSummaryHex", Stat,

compute_group = function(data, scales, binwidth = NULL, bins = 30, drop = TRUE,
fun = "mean", fun.args = list()) {
try_require("hexbin", "stat_summary_hex")
check_installed("hexbin", reason = "for `stat_summary_hex()`")

binwidth <- binwidth %||% hex_binwidth(bins, scales)
fun <- as_function(fun)
Expand Down
3 changes: 1 addition & 2 deletions R/stat-summary.r
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ NULL
wrap_hmisc <- function(fun) {

function(x, ...) {
if (!requireNamespace("Hmisc", quietly = TRUE))
abort("Hmisc package required for this function")
check_installed("Hmisc")

fun <- getExportedValue("Hmisc", fun)
result <- do.call(fun, list(x = quote(x), ...))
Expand Down
16 changes: 0 additions & 16 deletions R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,6 @@ clist <- function(l) {
paste(paste(names(l), l, sep = " = ", collapse = ", "), sep = "")
}


# Test whether package `package` is available. `fun` provides
# the name of the ggplot2 function that uses this package, and is
# used only to produce a meaningful error message if the
# package is not available.
try_require <- function(package, fun) {
if (requireNamespace(package, quietly = TRUE)) {
return(invisible())
}

abort(glue("
Package `{package}` required for `{fun}`.
Please install and try again.
"))
}

# Return unique columns
# This is used for figuring out which columns are constant within a group
#
Expand Down