Skip to content

Commit 5824b1d

Browse files
committed
fallback to method = "lm" in absence of {mgcv}
1 parent ec75f37 commit 5824b1d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

R/stat-smooth.R

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ StatSmooth <- ggproto("StatSmooth", Stat,
111111
}
112112

113113
if (is.null(params$formula)) {
114-
if (identical(method, "gam")) {
114+
if (identical(method, "gam") && is_installed("mgcv")) {
115115
params$formula <- y ~ s(x, bs = "cs")
116116
} else {
117117
params$formula <- y ~ x
@@ -133,7 +133,15 @@ StatSmooth <- ggproto("StatSmooth", Stat,
133133
}
134134
# If gam and gam's method is not specified by the user then use REML
135135
if (identical(method, gam_method())) {
136-
params$method.args$method <- params$method.args$method %||% "REML"
136+
if (identical(method, stats::lm)) {
137+
cli::cli_warn(c(
138+
"The {.pkg mgcv} package must be installed to use \\
139+
{.code method = \"gam\"}.",
140+
"Falling back to {.code method = \"lm\"}."
141+
))
142+
} else {
143+
params$method.args$method <- params$method.args$method %||% "REML"
144+
}
137145
}
138146

139147
if (length(msg) > 0) {
@@ -204,4 +212,10 @@ StatSmooth <- ggproto("StatSmooth", Stat,
204212
)
205213

206214
# This function exists to silence an undeclared import warning
207-
gam_method <- function() mgcv::gam
215+
gam_method <- function() {
216+
if (is_installed("mgcv")) {
217+
mgcv::gam
218+
} else {
219+
stats::lm
220+
}
221+
}

0 commit comments

Comments
 (0)