@@ -74,11 +74,81 @@ model_printer <- function(x, ...) {
74
74
}
75
75
}
76
76
}
77
+ if (! has_loaded_implementation(x )) {
78
+ cat(prompt_missing_implementation(x ), fill = 80 )
79
+ }
77
80
}
78
81
79
82
is_missing_arg <- function (x )
80
83
identical(x , quote(missing_arg()))
81
84
85
+ # given a model object, return TRUE if:
86
+ # * the model is supported without extensions
87
+ # * the model needs an extension and it is loaded
88
+ #
89
+ # return FALSE if:
90
+ # * the model needs an extension and it is _not_ loaded
91
+ has_loaded_implementation <- function (x ) {
92
+ spec_ <- class(x )[1 ]
93
+ mode_ <- x $ mode
94
+
95
+ if (mode_ == " unknown" ) {
96
+ mode_ <- c(" regression" , " censored regression" , " classification" )
97
+ }
98
+
99
+ avail <-
100
+ show_engines(spec_ ) %> %
101
+ dplyr :: filter(mode %in% mode_ )
102
+ pars <-
103
+ utils :: read.delim(system.file(" models.tsv" , package = " parsnip" )) %> %
104
+ dplyr :: filter(model == spec_ , mode %in% mode_ , is.na(pkg ))
105
+
106
+ if (nrow(pars ) > 0 || nrow(avail ) > 0 ) {
107
+ return (TRUE )
108
+ }
109
+
110
+ FALSE
111
+ }
112
+
113
+ # construct a message informing the user that there are no
114
+ # implementations for the current model spec / mode / engine.
115
+ #
116
+ # if there's a "pre-registered" extension supporting that setup,
117
+ # nudge the user to install/load it.
118
+ prompt_missing_implementation <- function (x ) {
119
+ spec_ <- class(x )[1 ]
120
+ engine_ <- x $ engine
121
+ mode_ <- x $ mode
122
+
123
+ avail <-
124
+ show_engines(spec_ ) %> %
125
+ dplyr :: filter(mode == mode )
126
+ all <-
127
+ utils :: read.delim(system.file(" models.tsv" , package = " parsnip" )) %> %
128
+ dplyr :: filter(model == spec_ , mode == mode , ! is.na(pkg )) %> %
129
+ dplyr :: select(- model )
130
+
131
+ msg <-
132
+ glue :: glue(
133
+ " A parsnip implementation for `{spec_}` {mode_} model " ,
134
+ " specifications using the `{engine_}` engine is not loaded."
135
+ )
136
+
137
+ if (nrow(avail ) == 0 && nrow(all ) > 0 ) {
138
+ msg <-
139
+ glue :: glue_collapse(c(
140
+ msg ,
141
+ glue :: glue_collapse(c(
142
+ " \n The parsnip extension package {" ,
143
+ cli :: col_yellow(all $ pkg [[1 ]]),
144
+ " } implements support for this specification/mode/engine combination. \n " ,
145
+ " Please install (if needed) and load to continue."
146
+ ))
147
+ ))
148
+ }
149
+
150
+ msg
151
+ }
82
152
83
153
# ' Print the model call
84
154
# '
@@ -89,18 +159,10 @@ is_missing_arg <- function(x)
89
159
show_call <- function (object ) {
90
160
object $ method $ fit $ args <-
91
161
map(object $ method $ fit $ args , convert_arg )
92
- if (
93
- is.null(object $ method $ fit $ func [" pkg" ]) ||
94
- is.na(object $ method $ fit $ func [" pkg" ])
95
- ) {
96
- res <- call2(object $ method $ fit $ func [" fun" ], !!! object $ method $ fit $ args )
97
- } else {
98
- res <-
99
- call2(object $ method $ fit $ func [" fun" ],
100
- !!! object $ method $ fit $ args ,
101
- .ns = object $ method $ fit $ func [" pkg" ])
102
- }
103
- res
162
+
163
+ call2(object $ method $ fit $ func [" fun" ],
164
+ !!! object $ method $ fit $ args ,
165
+ .ns = object $ method $ fit $ func [" pkg" ])
104
166
}
105
167
106
168
convert_arg <- function (x ) {
@@ -110,7 +172,6 @@ convert_arg <- function(x) {
110
172
x
111
173
}
112
174
113
-
114
175
levels_from_formula <- function (f , dat ) {
115
176
if (inherits(dat , " tbl_spark" ))
116
177
res <- NULL
0 commit comments