@@ -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