Skip to content

Commit 124dab9

Browse files
committed
changes for cran submission
1 parent 9ea567a commit 124dab9

File tree

8 files changed

+38
-24
lines changed

8 files changed

+38
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.DS_Store
55
tests/testthat/derby.log
66
tests/testthat/logs/
7+
*.history

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: parsnip
22
Version: 0.0.1
33
Title: A Common API to Modeling and Analysis Functions
4-
Description: A common interface is provided to allow users to specify a model without having to remember the different argument names across different functions or computational engines (e.g. R, Spark, Stan, etc).
4+
Description: A common interface is provided to allow users to specify a model without having to remember the different argument names across different functions or computational engines (e.g. 'R', 'Spark', 'Stan', etc).
55
Authors@R: c(
66
person("Max", "Kuhn", , "[email protected]", c("aut", "cre")),
77
person("Davis", "Vaughan", , "[email protected]", c("aut")),

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,4 @@ importFrom(utils,capture.output)
190190
importFrom(utils,getFromNamespace)
191191
importFrom(utils,globalVariables)
192192
importFrom(utils,head)
193-
importFrom(utils,installed.packages)
194193
importFrom(utils,stack)

R/engines.R

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,43 @@ check_engine <- function(object) {
3838
object
3939
}
4040

41-
#' @importFrom utils installed.packages
41+
# ------------------------------------------------------------------------------
42+
43+
shhhh <- function(x)
44+
suppressPackageStartupMessages(requireNamespace(x, quietly = TRUE))
45+
46+
is_installed <- function(pkg) {
47+
res <- try(shhhh(pkg), silent = TRUE)
48+
res
49+
}
50+
51+
#' @importFrom purrr map_lgl
4252
check_installs <- function(x) {
43-
if (length(x$method$library) > 0) {
44-
lib_inst <- rownames(installed.packages())
45-
is_inst <- x$method$library %in% lib_inst
53+
if (length(x$method$libs) > 0) {
54+
is_inst <- map_lgl(x$method$libs, is_installed)
4655
if (any(!is_inst)) {
4756
stop(
4857
"This engine requires some package installs: ",
49-
paste0("'", x$method$library[!is_inst], "'", collapse = ", "),
58+
paste0("'", x$method$libs[!is_inst], "'", collapse = ", "),
5059
call. = FALSE
5160
)
5261
}
5362
}
5463
}
5564

65+
load_libs <- function(x, quiet, attach = FALSE) {
66+
for (pkg in x$method$libs) {
67+
if (!attach) {
68+
suppressPackageStartupMessages(requireNamespace(pkg, quietly = quiet))
69+
} else {
70+
library(pkg, character.only = TRUE)
71+
}
72+
}
73+
invisible(x)
74+
}
75+
76+
# ------------------------------------------------------------------------------
77+
5678
#' Declare a computational engine and specific arguments
5779
#'
5880
#' `set_engine` is used to specify which package or system will be used

R/fit.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fit.model_spec <-
120120

121121
check_installs(object) # TODO rewrite with pkgman
122122

123-
load_libs(object, control$verbosity < 2)
123+
check_installs(object)
124124

125125
interfaces <- paste(fit_interface, object$method$fit$interface, sep = "_")
126126

@@ -199,7 +199,7 @@ fit_xy.model_spec <-
199199

200200
check_installs(object) # TODO rewrite with pkgman
201201

202-
load_libs(object, control$verbosity < 2)
202+
check_installs(object)
203203

204204
interfaces <- paste(fit_interface, object$method$fit$interface, sep = "_")
205205

R/mars.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ update.mars <-
110110
num_terms = NULL, prod_degree = NULL, prune_method = NULL,
111111
fresh = FALSE, ...) {
112112
update_dot_check(...)
113+
113114
args <- list(
114115
num_terms = enquo(num_terms),
115116
prod_degree = enquo(prod_degree),
@@ -180,6 +181,7 @@ check_args.mars <- function(object) {
180181

181182
#' @importFrom purrr map_dfr
182183
earth_submodel_pred <- function(object, new_data, terms = 2:3, ...) {
184+
load_libs(object, quiet = TRUE, attach = TRUE)
183185
map_dfr(terms, earth_reg_updater, object = object, newdata = new_data, ...)
184186
}
185187

@@ -208,7 +210,9 @@ multi_predict._earth <-
208210
function(object, new_data, type = NULL, num_terms = NULL, ...) {
209211
if (any(names(enquos(...)) == "newdata"))
210212
stop("Did you mean to use `new_data` instead of `newdata`?", call. = FALSE)
211-
213+
214+
load_libs(object, quiet = TRUE, attach = TRUE)
215+
212216
if (is.null(num_terms))
213217
num_terms <- object$fit$selected.terms[-1]
214218

@@ -232,9 +236,6 @@ multi_predict._earth <-
232236
} else
233237
stop (msg, call. = FALSE)
234238

235-
if (!exists("earth"))
236-
suppressPackageStartupMessages(attachNamespace("earth"))
237-
238239
if (is.null(type)) {
239240
if (object$spec$mode == "classification")
240241
type <- "class"

R/misc.R

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,6 @@ model_printer <- function(x, ...) {
7979
}
8080
}
8181

82-
load_libs <- function(x, quiet, attach = FALSE) {
83-
for (pkg in x$method$libs) {
84-
if(attach) {
85-
suppressPackageStartupMessages(requireNamespace(pkg, quietly = quiet))
86-
} else {
87-
library(pkg, character.only = TRUE)
88-
}
89-
}
90-
invisible(x)
91-
}
92-
9382
is_missing_arg <- function(x)
9483
identical(x, quote(missing_arg()))
9584

tests/testthat/test_mars.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,13 @@ test_that('submodel prediction', {
217217
set_engine("earth", keepxy = TRUE) %>%
218218
fit(mpg ~ ., data = mtcars[-(1:4), ])
219219

220+
parsnip:::load_libs(reg_fit$spec, quiet = TRUE, attach = TRUE)
220221
tmp_reg <- reg_fit$fit
221222
tmp_reg$call[["pmethod"]] <- eval_tidy(tmp_reg$call[["pmethod"]])
222223
tmp_reg$call[["keepxy"]] <- eval_tidy(tmp_reg$call[["keepxy"]])
223224
tmp_reg$call[["nprune"]] <- eval_tidy(tmp_reg$call[["nprune"]])
224225

226+
225227
pruned_reg <- update(tmp_reg, nprune = 5)
226228
pruned_reg_pred <- predict(pruned_reg, mtcars[1:4, -1])[,1]
227229

0 commit comments

Comments
 (0)