Skip to content

Commit 91b6a17

Browse files
committed
load quantreg only if absolutely necessary
1 parent 5f609a3 commit 91b6a17

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

R/stat-quantile.r

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ 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", attach = TRUE)
53+
try_require("quantreg", "stat_quantile")
5454

5555
if (is.null(formula)) {
5656
if (method == "rqss") {
57-
try_require("MatrixModels", "stat_quantile", attach = TRUE)
57+
# this should not be needed, since quantreg imports MatrixModels
58+
# try_require("MatrixModels", "stat_quantile")
59+
60+
# we need to attach quantreg for qss to work inside formula
61+
require("quantreg")
5862
formula <- eval(substitute(y ~ qss(x, lambda = lambda)),
5963
list(lambda = lambda))
6064
} else {
@@ -73,7 +77,13 @@ 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 (is.character(method)) {
83+
if (identical(method, "rq")) method <- quantreg::rq
84+
else if (identical(method, "rqss")) method <- quantreg::rqss
85+
else method <- match.fun(method) # allow users to supply their own methods
86+
}
7787

7888
rbind_dfs(lapply(quantiles, quant_pred, data = data, method = method,
7989
formula = formula, weight = weight, grid = grid, method.args = method.args))

0 commit comments

Comments
 (0)