Skip to content

Commit 4b04aa6

Browse files
committed
add machinery to prompt on unavailable extension package
1 parent 64e7125 commit 4b04aa6

File tree

1 file changed

+74
-13
lines changed

1 file changed

+74
-13
lines changed

R/misc.R

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,81 @@ model_printer <- function(x, ...) {
7474
}
7575
}
7676
}
77+
if (!has_loaded_implementation(x)) {
78+
cat(prompt_missing_implementation(x), fill = 80)
79+
}
7780
}
7881

7982
is_missing_arg <- function(x)
8083
identical(x, quote(missing_arg()))
8184

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+
"\nThe 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+
}
82152

83153
#' Print the model call
84154
#'
@@ -89,18 +159,10 @@ is_missing_arg <- function(x)
89159
show_call <- function(object) {
90160
object$method$fit$args <-
91161
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"])
104166
}
105167

106168
convert_arg <- function(x) {
@@ -110,7 +172,6 @@ convert_arg <- function(x) {
110172
x
111173
}
112174

113-
114175
levels_from_formula <- function(f, dat) {
115176
if (inherits(dat, "tbl_spark"))
116177
res <- NULL

0 commit comments

Comments
 (0)