Skip to content

Commit 409fdf1

Browse files
committed
new structure working up to fit and fit_xy
1 parent de5ea8b commit 409fdf1

File tree

5 files changed

+21
-59
lines changed

5 files changed

+21
-59
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# parsnip 0.0.2.9000
22

3+
## Breaking Changes
4+
5+
* The method that `parsnip` stores the model information has changed. Any custom models from previous versions will need to use the new method for registering models.
6+
* The mode need to be declared for models that can be used for more than one mode prior to fitting and/or translation).
7+
38
## New Features
49

510
* `add_rowindex()` can create a column called `.row` to a data frame.

R/engines.R

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11

2-
get_model_info <- function (x, engine) {
3-
cls <- specific_model(x)
4-
nm <- paste(cls, engine, "data", sep = "_")
5-
res <- try(get(nm), silent = TRUE)
6-
if (inherits(res, "try-error"))
7-
stop("Can't find model object ", nm)
8-
res
9-
}
10-
112
specific_model <- function(x) {
123
cls <- class(x)
134
cls[cls != "model_spec"]
145
}
156

16-
177
possible_engines <- function(object, ...) {
188
m_env <- get_model_env()
199
engs <- rlang::env_get(m_env, specific_model(object))

R/fit.R

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
#'
5353
#' lr_mod <- logistic_reg()
5454
#'
55-
#' lr_mod <- logistic_reg()
56-
#'
5755
#' using_formula <-
5856
#' lr_mod %>%
5957
#' set_engine("glm") %>%
@@ -125,7 +123,7 @@ fit.model_spec <-
125123
)
126124

127125
# populate `method` with the details for this model type
128-
object <- get_method(object, engine = object$engine)
126+
object <- add_methods(object, engine = object$engine)
129127

130128
check_installs(object)
131129

@@ -215,7 +213,7 @@ fit_xy.model_spec <-
215213
)
216214

217215
# populate `method` with the details for this model type
218-
object <- get_method(object, engine = object$engine)
216+
object <- add_methods(object, engine = object$engine)
219217

220218
check_installs(object)
221219

@@ -239,7 +237,7 @@ fit_xy.model_spec <-
239237
...
240238
),
241239

242-
data.frame_data.frame =, matrix_data.frame =
240+
data.frame_data.frame = , matrix_data.frame =
243241
xy_xy(
244242
object = object,
245243
env = eval_env,
@@ -249,7 +247,7 @@ fit_xy.model_spec <-
249247
),
250248

251249
# heterogenous combinations
252-
matrix_formula =, data.frame_formula =
250+
matrix_formula = , data.frame_formula =
253251
xy_form(
254252
object = object,
255253
env = eval_env,
@@ -367,7 +365,7 @@ check_xy_interface <- function(x, y, cl, model) {
367365
print.model_fit <- function(x, ...) {
368366
cat("parsnip model object\n\n")
369367

370-
if(inherits(x$fit, "try-error")) {
368+
if (inherits(x$fit, "try-error")) {
371369
cat("Model fit failed with error:\n", x$fit, "\n")
372370
} else {
373371
print(x$fit, ...)

R/translate.R

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#'
4242
#' @export
4343

44-
translate <- function (x, ...)
44+
translate <- function(x, ...)
4545
UseMethod("translate")
4646

4747
#' @importFrom utils getFromNamespace
@@ -58,12 +58,10 @@ translate.default <- function(x, engine = x$engine, ...) {
5858
x$engine <- engine
5959
x <- check_engine(x)
6060

61-
# TODOS
62-
# what to do with unknown mode?
6361
if (x$mode == "unknown") {
6462
stop("Model code depends on the mode; please specify one.", call. = FALSE)
6563
}
66-
# set the classes. Is a constructor not being used?
64+
6765
if (is.null(x$method))
6866
x$method <- get_model_spec(mod_name, x$mode, engine)
6967

@@ -78,11 +76,11 @@ translate.default <- function(x, engine = x$engine, ...) {
7876
x$eng_args <- check_eng_args(x$eng_args, x$method$fit, arg_key$original)
7977

8078
# keep only modified args
81-
modifed_args <- purrr::map_lgl(actual_args, null_value)
79+
modifed_args <- !purrr::map_lgl(actual_args, null_value)
8280
actual_args <- actual_args[modifed_args]
8381

8482
# look for defaults if not modified in other
85-
if(length(x$method$fit$defaults) > 0) {
83+
if (length(x$method$fit$defaults) > 0) {
8684
in_other <- names(x$method$fit$defaults) %in% names(x$eng_args)
8785
x$defaults <- x$method$fit$defaults[!in_other]
8886
}
@@ -96,40 +94,6 @@ translate.default <- function(x, engine = x$engine, ...) {
9694
x
9795
}
9896

99-
get_method <- function(x, engine = x$engine, ...) {
100-
check_empty_ellipse(...)
101-
x$engine <- engine
102-
x <- check_engine(x)
103-
x$method <- get_model_info(x, x$engine)
104-
x
105-
}
106-
107-
108-
get_module <- function(nm) {
109-
arg_key <- try(
110-
getFromNamespace(
111-
paste0(nm, "_arg_key"),
112-
ns = "parsnip"
113-
),
114-
silent = TRUE
115-
)
116-
if(inherits(arg_key, "try-error")) {
117-
arg_key <- try(
118-
get(paste0(nm, "_arg_key")),
119-
silent = TRUE
120-
)
121-
}
122-
if(inherits(arg_key, "try-error")) {
123-
stop(
124-
"Cannot find the model code: `",
125-
paste0(nm, "_arg_key"),
126-
"`", call. = FALSE
127-
)
128-
}
129-
arg_key
130-
}
131-
132-
13397
#' @export
13498
print.model_spec <- function(x, ...) {
13599
cat("Model Specification (", x$mode, ")\n\n", sep = "")
@@ -201,3 +165,10 @@ unionize <- function(args, key) {
201165
names(args) <- merged$original
202166
args[!is.na(merged$original)]
203167
}
168+
169+
add_methods <- function(x, engine) {
170+
x$engine <- engine
171+
x <- check_engine(x)
172+
x$method <- get_model_spec(specific_model(x), x$mode, x$engine)
173+
x
174+
}

man/fit.Rd

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

0 commit comments

Comments
 (0)