Skip to content

Commit 031da1d

Browse files
committed
user code to determine required packages
1 parent aa29bac commit 031da1d

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

R/req_pkgs.R

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

man/req_pkgs.Rd

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_packages.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
context("checking for packages")
3+
load(test_path("mars_model.RData"))
4+
5+
# ------------------------------------------------------------------------------
6+
7+
test_that('required packages', {
8+
9+
expect_error(req_pkgs(linear_reg()), "Please set an engine")
10+
11+
glmn <-
12+
linear_reg() %>%
13+
set_engine("glmnet") %>%
14+
req_pkgs()
15+
expect_equal(glmn, "glmnet")
16+
17+
lm_fit <-
18+
linear_reg() %>%
19+
set_engine("lm") %>%
20+
fit(mpg ~ ., data = mtcars) %>%
21+
req_pkgs()
22+
expect_equal(lm_fit, "stats")
23+
})
24+
25+
# ------------------------------------------------------------------------------
26+
27+
test_that('missing packages', {
28+
has_earth <- parsnip:::is_installed("earth")
29+
30+
if (has_earth) {
31+
expect_error(predict(mars_model, mtcars[1:3, -1]), regexp = NA)
32+
33+
} else {
34+
expect_error(predict(mars_model, mtcars[1:3, -1]), regexp = "earth")
35+
expect_true(any(names(sessionInfo()$loadedOnly) == "earth"))
36+
}
37+
mars_model$spec$method$libs <- "rootveggie"
38+
expect_error(predict(mars_model, mtcars[1:3, -1]), regexp = "rootveggie")
39+
40+
})
41+

0 commit comments

Comments
 (0)