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
6260fit.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
9391fit_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
185183formula_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
221218recipe_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
0 commit comments