@@ -95,36 +95,63 @@ StatSmooth <- ggproto("StatSmooth", Stat,
9595 setup_params = function (data , params ) {
9696 params $ flipped_aes <- has_flipped_aes(data , params , ambiguous = TRUE )
9797 msg <- character ()
98- if (is.null(params $ method ) || identical(params $ method , " auto" )) {
98+ method <- params $ method
99+ if (is.null(method ) || identical(method , " auto" )) {
99100 # Use loess for small datasets, gam with a cubic regression basis for
100101 # larger. Based on size of the _largest_ group to avoid bad memory
101102 # behaviour of loess
102103 max_group <- max(table(interaction(data $ group , data $ PANEL , drop = TRUE )))
103104
104105 if (max_group < 1000 ) {
105- params $ method <- " loess"
106+ method <- " loess"
106107 } else {
107- params $ method <- " gam"
108+ method <- " gam"
108109 }
109- msg <- c(msg , paste0(" method = '" , params $ method , " '" ))
110+ msg <- c(msg , paste0(" method = '" , method , " '" ))
111+ }
112+
113+ if (identical(method , " gam" ) &&
114+ ! prompt_install(" mgcv" , " for using {.code method = \" gam\" }" )) {
115+ cli :: cli_inform(c(
116+ " The {.arg method} was set to {.val gam}, but {.pkg mgcv} is not installed." ,
117+ " !" = " Falling back to {.code method = \" lm\" }." ,
118+ i = " Install {.pkg mgcv} or change the {.arg method} argument to \\
119+ resolve this issue."
120+ ))
121+ method <- " lm"
110122 }
111123
112124 if (is.null(params $ formula )) {
113- if (identical(params $ method , " gam" )) {
125+ if (identical(method , " gam" )) {
114126 params $ formula <- y ~ s(x , bs = " cs" )
115127 } else {
116128 params $ formula <- y ~ x
117129 }
118130 msg <- c(msg , paste0(" formula = '" , deparse(params $ formula ), " '" ))
119131 }
120- if (identical(params $ method , " gam" )) {
121- params $ method <- gam_method()
132+
133+ # Special case span because it's the most commonly used model argument
134+ if (identical(method , " loess" )) {
135+ params $ method.args $ span <- params $ span %|| % 0.75
136+ }
137+
138+ if (is.character(method )) {
139+ if (identical(method , " gam" )) {
140+ method <- gam_method()
141+ } else {
142+ method <- match.fun(method )
143+ }
144+ }
145+ # If gam and gam's method is not specified by the user then use REML
146+ if (identical(method , gam_method())) {
147+ params $ method.args $ method <- params $ method.args $ method %|| % " REML"
122148 }
123149
124150 if (length(msg ) > 0 ) {
125151 cli :: cli_inform(" {.fn geom_smooth} using {msg}" )
126152 }
127153
154+ params $ method <- method
128155 params
129156 },
130157
@@ -159,23 +186,6 @@ StatSmooth <- ggproto("StatSmooth", Stat,
159186 }
160187 }
161188
162- # Special case span because it's the most commonly used model argument
163- if (identical(method , " loess" )) {
164- method.args $ span <- span
165- }
166-
167- if (is.character(method )) {
168- if (identical(method , " gam" )) {
169- method <- gam_method()
170- } else {
171- method <- match.fun(method )
172- }
173- }
174- # If gam and gam's method is not specified by the user then use REML
175- if (identical(method , gam_method()) && is.null(method.args $ method )) {
176- method.args $ method <- " REML"
177- }
178-
179189 prediction <- try_fetch(
180190 {
181191 model <- inject(method(
@@ -205,4 +215,10 @@ StatSmooth <- ggproto("StatSmooth", Stat,
205215)
206216
207217# This function exists to silence an undeclared import warning
208- gam_method <- function () mgcv :: gam
218+ gam_method <- function () {
219+ if (is_installed(" mgcv" )) {
220+ mgcv :: gam
221+ } else {
222+ NA
223+ }
224+ }
0 commit comments