Skip to content

Commit 2e33301

Browse files
committed
error catching tunred off by defualt; also fixed environments for evaluation
1 parent 3db630d commit 2e33301

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ importFrom(recipes,prep)
2222
importFrom(rlang,enquo)
2323
importFrom(rlang,eval_tidy)
2424
importFrom(rlang,expr)
25+
importFrom(rlang,get_env)
2526
importFrom(rlang,is_empty)
2627
importFrom(rlang,is_missing)
2728
importFrom(rlang,is_null)

R/fit.R

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# General TODOs
22
# - think about case weights in each instance below
3-
# - try/catch all model fit evaluations
4-
# - option to capture output/verboseness
53
# - devise a unit test plan that does not add pkg deps for each model
64
# - where/how to add data checks (e.g. factors for classification)
75

@@ -60,7 +58,7 @@ fit <- function (object, ...)
6058
#' @export
6159
#' @rdname fit
6260
fit.model_spec <- function(object, x, engine = object$engine,
63-
.control = list(verbosity = 1, catch = TRUE),
61+
.control = list(verbosity = 1, catch = FALSE),
6462
...) {
6563
object$engine <- engine
6664
object <- check_engine(object)
@@ -88,7 +86,7 @@ fit.model_spec <- function(object, x, engine = object$engine,
8886

8987
###################################################################
9088

91-
#' @importFrom rlang eval_tidy quos
89+
#' @importFrom rlang eval_tidy quos get_env
9290
#' @importFrom stats as.formula
9391
fit_formula <- function(object, formula, engine = engine, .control, ...) {
9492
opts <- quos(...)
@@ -135,7 +133,7 @@ fit_xy <- function(object, x, .control, ...) {
135133
} else {
136134
if(object$method$interface %in% c("data.frame", "matrix")) {
137135
fit_expr <- sub_arg_values(object$method$fit, opts["y"])
138-
res <- eval_mod(fit_expr, capture = .control$verbosity == 0, catch = .control$catch)
136+
res <- eval_mod(fit_expr, capture = .control$verbosity == 0, catch = .control$catch, env = get_env())
139137
} else {
140138
stop("I don't know about the ",
141139
object$method$interface, " interface.",
@@ -183,17 +181,16 @@ formula_to_recipe <- function(object, formula, data, .control) {
183181

184182
#' @importFrom stats model.frame model.response terms
185183
formula_to_xy <- function(object, formula, data, .control) {
186-
# TODO how do we fill in the other standard things here (subset, contrasts etc)?
187-
# TODO add a "matrix" option here and invoke model.matrix
188-
184+
# Q: how do we fill in the other standard things here (subset, contrasts etc)?
185+
# Q: add a "matrix" option here and invoke model.matrix
189186
# Q: avoid eval using ?get_expr(data[["data"]])
190187
x <- stats::model.frame(formula, eval_tidy(data[["data"]]))
191188
y <- model.response(x)
192189
outcome_cols <- attr(terms(x), "response")
193190
if (!isTRUE(all.equal(outcome_cols, 0))) {
194191
x <- x[,-outcome_cols, drop = FALSE]
195192
}
196-
eval_mod(object$method$fit, capture = .control$verbosity == 0, catch = .control$catch)
193+
eval_mod(object$method$fit, capture = .control$verbosity == 0, catch = .control$catch, env = get_env())
197194
}
198195

199196
###################################################################
@@ -215,7 +212,7 @@ recipe_to_formula <- function(object, recipe, data, .control) {
215212
fit_expr <- object$method$fit
216213
fit_expr$formula <- as.formula(paste0(y_names, "~."))
217214
fit_expr$data <- quote(dat)
218-
eval_mod(fit_expr, capture = .control$verbosity == 0, catch = .control$catch)
215+
eval_mod(fit_expr, capture = .control$verbosity == 0, catch = .control$catch, env = get_env())
219216
}
220217

221218
recipe_to_xy <- function(object, recipe, data, .control) {
@@ -232,7 +229,7 @@ recipe_to_xy <- function(object, recipe, data, .control) {
232229
y <- y[[1]]
233230

234231
fit_expr <- object$method$fit
235-
eval_mod(fit_expr, capture = .control$verbosity == 0, catch = .control$catch)
232+
eval_mod(fit_expr, capture = .control$verbosity == 0, catch = .control$catch, env = get_env())
236233
}
237234

238235
###################################################################
@@ -254,18 +251,18 @@ xy_to_recipe <- function(object, x, y, .control) {
254251
###################################################################
255252

256253
#' @importFrom utils capture.output
257-
eval_mod <- function(e, capture = FALSE, catch = FALSE) {
254+
eval_mod <- function(e, capture = FALSE, catch = FALSE, ...) {
258255
if (capture) {
259256
if (catch) {
260-
junk <- capture.output(res <- try(eval_tidy(e), silent = TRUE))
257+
junk <- capture.output(res <- try(eval_tidy(e, ...), silent = TRUE))
261258
} else {
262-
junk <- capture.output(res <- eval_tidy(e))
259+
junk <- capture.output(res <- eval_tidy(e, ...))
263260
}
264261
} else {
265262
if (catch) {
266-
res <- try(eval_tidy(e), silent = TRUE)
263+
res <- try(eval_tidy(e, ...), silent = TRUE)
267264
} else {
268-
res <- eval_tidy(e)
265+
res <- eval_tidy(e, ...)
269266
}
270267
}
271268
res

R/rand_forest.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ print.rand_forest <- function(x, ...) {
104104
# will be used to create the specific model code given that a
105105
# computation engine has been declared.
106106

107-
## Q: If/when extra arguments are added to the call (that would be put
108-
## in the ellipses), when should the ellipses be removed? Maybe right
109-
## before evaluation since `update` might be invoked to change those.
110-
111107
rand_forest_ranger_regression <- function () {
112108
libs <- "ranger"
113109
interface <- "formula"

man/fit.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)