forked from tidymodels/parsnip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreq_pkgs.R
53 lines (49 loc) · 1.07 KB
/
req_pkgs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#' Determine required packages for a model
#'
#' @param x A model specification or fit.
#' @param ... Not used.
#' @return A character string of package names (if any).
#' @details
#' For a model specification, the engine must be set.
#'
#' The list does not include the `parsnip` package.
#' @examples
#' should_fail <- try(req_pkgs(linear_reg()), silent = TRUE)
#' should_fail
#'
#' linear_reg() %>%
#' set_engine("glmnet") %>%
#' req_pkgs()
#'
#' linear_reg() %>%
#' set_engine("lm") %>%
#' fit(mpg ~ ., data = mtcars) %>%
#' req_pkgs()
#' @export
req_pkgs <- function(x, ...) {
UseMethod("req_pkgs")
}
#' @export
#' @rdname req_pkgs
req_pkgs.model_spec <- function(x, ...) {
if (is.null(x$engine)) {
rlang::abort("Please set an engine.")
}
get_pkgs(x)
}
#' @export
#' @rdname req_pkgs
req_pkgs.model_fit <- function(x, ...) {
get_pkgs(x$spec)
}
get_pkgs <- function(x) {
cls <- class(x)[1]
pkgs <-
get_from_env(paste0(cls, "_pkgs")) %>%
dplyr::filter(engine == x$engine)
res <- pkgs$pkg[[1]]
if (length(res) == 0) {
res <- character(0)
}
res
}