Skip to content

Commit be50f53

Browse files
Use rlang::check_installed() (#4375)
* bump rlang dep * requireNamespace -> check_installed * try_require -> check_installed * check mapproj installed * add reasons * update news
1 parent 4555055 commit be50f53

File tree

10 files changed

+13
-29
lines changed

10 files changed

+13
-29
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Imports:
3535
isoband,
3636
MASS,
3737
mgcv,
38-
rlang (>= 0.3.0),
38+
rlang (>= 0.4.10),
3939
scales (>= 0.5.0),
4040
stats,
4141
tibble,

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525
* ggplot2 now requires R >= 3.3 (#4247).
2626

27+
* ggplot2 now uses `rlang::check_installed()` to check if a suggested package is
28+
installed, which will offer to install the package before continuing (#4375,
29+
@malcolmbarrett)
30+
2731
# ggplot2 3.3.3
2832
This is a small patch release mainly intended to address changes in R and CRAN.
2933
It further changes the licensing model of ggplot2 to an MIT license.

R/coord-map.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ CoordMap <- ggproto("CoordMap", Coord,
318318

319319

320320
mproject <- function(coord, x, y, orientation) {
321+
check_installed("mapproj", reason = "for `coord_map()`")
321322
suppressWarnings(mapproj::mapproject(x, y,
322323
projection = coord$projection,
323324
parameters = coord$params,

R/fortify-map.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fortify.map <- function(model, data, ...) {
7575
#' coord_map("albers", lat0 = 45.5, lat1 = 29.5)
7676
#' }
7777
map_data <- function(map, region = ".", exact = FALSE, ...) {
78-
try_require("maps", "map_data")
78+
check_installed("maps", reason = "for `map_data()`")
7979
map_obj <- maps::map(map, region, exact = exact, plot = FALSE, fill = TRUE, ...)
8080
fortify(map_obj)
8181
}

R/fortify.r

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ fortify.data.frame <- function(model, data, ...) model
1717
fortify.tbl_df <- function(model, data, ...) model
1818
#' @export
1919
fortify.tbl <- function(model, data, ...) {
20-
if (!requireNamespace("dplyr", quietly = TRUE)) {
21-
abort("dplyr must be installed to work with tbl objects")
22-
}
20+
check_installed("dplyr", reason = "to work with `tbl` objects")
2321
dplyr::collect(model)
2422
}
2523
#' @export
@@ -31,9 +29,7 @@ fortify.function <- function(model, data, ...) model
3129
fortify.formula <- function(model, data, ...) as_function(model)
3230
#' @export
3331
fortify.grouped_df <- function(model, data, ...) {
34-
if (!requireNamespace("dplyr", quietly = TRUE)) {
35-
abort("dplyr must be installed to work with grouped_df objects")
36-
}
32+
check_installed("dplyr", reason = "to work with `grouped_df` objects")
3733
model$.group <- dplyr::group_indices(model)
3834
model
3935
}

R/stat-binhex.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ StatBinhex <- ggproto("StatBinhex", Stat,
4949

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

5454
binwidth <- binwidth %||% hex_binwidth(bins, scales)
5555
wt <- data$weight %||% rep(1L, nrow(data))

R/stat-quantile.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ StatQuantile <- ggproto("StatQuantile", Stat,
5050
compute_group = function(data, scales, quantiles = c(0.25, 0.5, 0.75),
5151
formula = NULL, xseq = NULL, method = "rq",
5252
method.args = list(), lambda = 1, na.rm = FALSE) {
53-
try_require("quantreg", "stat_quantile")
53+
check_installed("quantreg", reason = "for `stat_quantile()`")
5454

5555
if (is.null(formula)) {
5656
if (method == "rqss") {

R/stat-summary-hex.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ StatSummaryHex <- ggproto("StatSummaryHex", Stat,
4343

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

4848
binwidth <- binwidth %||% hex_binwidth(bins, scales)
4949
fun <- as_function(fun)

R/stat-summary.r

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ NULL
245245
wrap_hmisc <- function(fun) {
246246

247247
function(x, ...) {
248-
if (!requireNamespace("Hmisc", quietly = TRUE))
249-
abort("Hmisc package required for this function")
248+
check_installed("Hmisc")
250249

251250
fun <- getExportedValue("Hmisc", fun)
252251
result <- do.call(fun, list(x = quote(x), ...))

R/utilities.r

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,6 @@ clist <- function(l) {
5656
paste(paste(names(l), l, sep = " = ", collapse = ", "), sep = "")
5757
}
5858

59-
60-
# Test whether package `package` is available. `fun` provides
61-
# the name of the ggplot2 function that uses this package, and is
62-
# used only to produce a meaningful error message if the
63-
# package is not available.
64-
try_require <- function(package, fun) {
65-
if (requireNamespace(package, quietly = TRUE)) {
66-
return(invisible())
67-
}
68-
69-
abort(glue("
70-
Package `{package}` required for `{fun}`.
71-
Please install and try again.
72-
"))
73-
}
74-
7559
# Return unique columns
7660
# This is used for figuring out which columns are constant within a group
7761
#

0 commit comments

Comments
 (0)