Skip to content

Commit 03bd946

Browse files
authored
Don't attach external packages (#3134)
* don't attach external packages; step 1: don't attach hexbin * fix documentation for `geom_quantile()`. * load quantreg only if absolutely necessary * remove library() call from try_require(). closes #3126. * don't require quantreg * news item
1 parent 0155d3c commit 03bd946

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
lines to `element_blank()` now also works in `coord_sf()`
66
(@clauswilke, #2991, #2525).
77

8+
* ggplot2 no longer attaches any external packages when using functions that depend on
9+
packages that are suggested but not imported by ggplot2. The affected functions
10+
include `geom_hex()`, `stat_binhex()`, `stat_summary_hex()`, `geom_quantile()`,
11+
`stat_quantile()`, and `map_data()` (@clauswilke, #3126).
12+
813
* `geom_hline()`, `geom_vline()`, and `geom_abline()` now throw a warning if the user supplies both an `xintercept`, `yintercept`, or `slope` value and a mapping (@RichardJActon, #2950).
914

1015
* `scale_color_continuous()` now points at `scale_colour_continuos()` so that it

R/stat-quantile.r

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' @param quantiles conditional quantiles of y to calculate and display
22
#' @param formula formula relating y variables to x variables
3-
#' @param method Quantile regression method to use. Currently only supports
4-
#' [quantreg::rq()].
3+
#' @param method Quantile regression method to use. Available options are `"rq"` (for
4+
#' [`quantreg::rq()`]) and `"rqss"` (for [`quantreg::rqss()`]).
55
#' @inheritParams layer
66
#' @inheritParams geom_point
77
#' @section Computed variables:
@@ -54,9 +54,13 @@ StatQuantile <- ggproto("StatQuantile", Stat,
5454

5555
if (is.null(formula)) {
5656
if (method == "rqss") {
57-
try_require("MatrixModels", "stat_quantile")
58-
formula <- eval(substitute(y ~ qss(x, lambda = lambda)),
59-
list(lambda = lambda))
57+
formula <- eval(
58+
substitute(y ~ qss(x, lambda = lambda)),
59+
list(lambda = lambda)
60+
)
61+
# make qss function available in case it is needed;
62+
# works around limitation in quantreg
63+
qss <- quantreg::qss
6064
} else {
6165
formula <- y ~ x
6266
}
@@ -73,7 +77,15 @@ StatQuantile <- ggproto("StatQuantile", Stat,
7377
}
7478
grid <- new_data_frame(list(x = xseq))
7579

76-
method <- match.fun(method)
80+
# if method was specified as a character string, replace with
81+
# the corresponding function
82+
if (identical(method, "rq")) {
83+
method <- quantreg::rq
84+
} else if (identical(method, "rqss")) {
85+
method <- quantreg::rqss
86+
} else {
87+
method <- match.fun(method) # allow users to supply their own methods
88+
}
7789

7890
rbind_dfs(lapply(quantiles, quant_pred, data = data, method = method,
7991
formula = formula, weight = weight, grid = grid, method.args = method.args))

R/utilities.r

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ clist <- function(l) {
4242
paste(paste(names(l), l, sep = " = ", collapse = ", "), sep = "")
4343
}
4444

45+
46+
# Test whether package `package` is available. `fun` provides
47+
# the name of the ggplot2 function that uses this package, and is
48+
# used only to produce a meaningful error message if the
49+
# package is not available.
4550
try_require <- function(package, fun) {
4651
if (requireNamespace(package, quietly = TRUE)) {
47-
library(package, character.only = TRUE)
4852
return(invisible())
4953
}
5054

man/geom_quantile.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)