Skip to content

Don't attach external packages #3134

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
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
lines to `element_blank()` now also works in `coord_sf()`
(@clauswilke, #2991, #2525).

* ggplot2 no longer attaches any external packages when using functions that depend on
packages that are suggested but not imported by ggplot2. The affected functions
include `geom_hex()`, `stat_binhex()`, `stat_summary_hex()`, `geom_quantile()`,
`stat_quantile()`, and `map_data()` (@clauswilke, #3126).

* `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).

* `scale_color_continuous()` now points at `scale_colour_continuos()` so that it
Expand Down
24 changes: 18 additions & 6 deletions R/stat-quantile.r
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @param quantiles conditional quantiles of y to calculate and display
#' @param formula formula relating y variables to x variables
#' @param method Quantile regression method to use. Currently only supports
#' [quantreg::rq()].
#' @param method Quantile regression method to use. Available options are `"rq"` (for
#' [`quantreg::rq()`]) and `"rqss"` (for [`quantreg::rqss()`]).
#' @inheritParams layer
#' @inheritParams geom_point
#' @section Computed variables:
Expand Down Expand Up @@ -54,9 +54,13 @@ StatQuantile <- ggproto("StatQuantile", Stat,

if (is.null(formula)) {
if (method == "rqss") {
try_require("MatrixModels", "stat_quantile")
formula <- eval(substitute(y ~ qss(x, lambda = lambda)),
list(lambda = lambda))
formula <- eval(
substitute(y ~ qss(x, lambda = lambda)),
list(lambda = lambda)
)
# make qss function available in case it is needed;
# works around limitation in quantreg
qss <- quantreg::qss
} else {
formula <- y ~ x
}
Expand All @@ -73,7 +77,15 @@ StatQuantile <- ggproto("StatQuantile", Stat,
}
grid <- new_data_frame(list(x = xseq))

method <- match.fun(method)
# if method was specified as a character string, replace with
# the corresponding function
if (identical(method, "rq")) {
method <- quantreg::rq
} else if (identical(method, "rqss")) {
method <- quantreg::rqss
} else {
method <- match.fun(method) # allow users to supply their own methods
}

rbind_dfs(lapply(quantiles, quant_pred, data = data, method = method,
formula = formula, weight = weight, grid = grid, method.args = method.args))
Expand Down
6 changes: 5 additions & 1 deletion R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ 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)) {
library(package, character.only = TRUE)
return(invisible())
}

Expand Down
4 changes: 2 additions & 2 deletions man/geom_quantile.Rd

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