Skip to content

Commit 233e12e

Browse files
committed
remove extraneous code in favour of NextMethod
1 parent 4acbeb0 commit 233e12e

File tree

2 files changed

+32
-136
lines changed

2 files changed

+32
-136
lines changed

R/epi_recipe.R

Lines changed: 24 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,10 @@ add_epi_recipe <- function(
9595
#' @rdname add_epi_recipe
9696
#' @export
9797
remove_epi_recipe <- function(x) {
98-
workflows:::validate_is_workflow(x)
99-
100-
if (!workflows:::has_preprocessor_recipe(x)) {
101-
rlang::warn("The workflow has no recipe preprocessor to remove.")
102-
}
103-
104-
actions <- x$pre$actions
105-
actions[["recipe"]] <- NULL
106-
107-
new_epi_workflow(
108-
pre = workflows:::new_stage_pre(actions = actions),
109-
fit = x$fit,
110-
post = x$post,
111-
trained = FALSE
112-
)
98+
workflows::remove_recipe(x)
11399
}
114100

101+
115102
#' @rdname add_epi_recipe
116103
#' @export
117104
update_epi_recipe <- function(x, recipe, ..., blueprint = default_epi_recipe_blueprint()) {
@@ -180,15 +167,21 @@ adjust_epi_recipe <- function(x, which_step, ..., blueprint = default_epi_recipe
180167

181168
#' @rdname adjust_epi_recipe
182169
#' @export
183-
adjust_epi_recipe.epi_workflow <- function(x, which_step, ..., blueprint = default_epi_recipe_blueprint()) {
184-
recipe <- adjust_epi_recipe(workflows::extract_preprocessor(x), which_step, ...)
170+
adjust_epi_recipe.epi_workflow <- function(
171+
x, which_step, ..., blueprint = default_epi_recipe_blueprint()
172+
) {
185173

186-
update_epi_recipe(x, recipe, blueprint = blueprint)
174+
rec <- adjust_epi_recipe(
175+
workflows::extract_preprocessor(x), which_step, ...
176+
)
177+
update_epi_recipe(x, rec, blueprint = blueprint)
187178
}
188179

189180
#' @rdname adjust_epi_recipe
190181
#' @export
191-
adjust_epi_recipe.epi_recipe <- function(x, which_step, ..., blueprint = default_epi_recipe_blueprint()) {
182+
adjust_epi_recipe.epi_recipe <- function(
183+
x, which_step, ..., blueprint = default_epi_recipe_blueprint()
184+
) {
192185
if (!(is.numeric(which_step) || is.character(which_step))) {
193186
cli::cli_abort(
194187
c("`which_step` must be a number or a character.",
@@ -294,109 +287,17 @@ kill_levels <- function(x, keys) {
294287

295288
#' @export
296289
print.epi_recipe <- function(x, form_width = 30, ...) {
297-
cli::cli_div(theme = list(.pkg = list("vec-trunc" = Inf, "vec-last" = ", ")))
298-
299-
cli::cli_h1("Epi Recipe")
300-
cli::cli_h3("Inputs")
301-
302-
tab <- table(x$var_info$role, useNA = "ifany")
303-
tab <- stats::setNames(tab, names(tab))
304-
names(tab)[is.na(names(tab))] <- "undeclared role"
305-
306-
roles <- c("outcome", "predictor", "case_weights", "undeclared role")
307-
308-
tab <- c(
309-
tab[names(tab) == roles[1]],
310-
tab[names(tab) == roles[2]],
311-
tab[names(tab) == roles[3]],
312-
sort(tab[!names(tab) %in% roles], TRUE),
313-
tab[names(tab) == roles[4]]
314-
)
315-
316-
cli::cli_text("Number of variables by role")
317-
318-
spaces_needed <- max(nchar(names(tab))) - nchar(names(tab)) +
319-
max(nchar(tab)) - nchar(tab)
320-
321-
cli::cli_verbatim(
322-
glue::glue("{names(tab)}: {strrep('\ua0', spaces_needed)}{tab}")
323-
)
324-
325-
if ("tr_info" %in% names(x)) {
326-
cli::cli_h3("Training information")
327-
nmiss <- x$tr_info$nrows - x$tr_info$ncomplete
328-
nrows <- x$tr_info$nrows
329-
330-
cli::cli_text(
331-
"Training data contained {nrows} data points and {cli::no(nmiss)} \\
332-
incomplete row{?s}."
333-
)
334-
}
335-
336-
if (!is.null(x$steps)) {
337-
cli::cli_h3("Operations")
338-
}
339-
340-
fmt <- cli::cli_fmt({
341-
for (step in x$steps) {
342-
print(step, form_width = form_width)
343-
}
344-
})
345-
cli::cli_ol(fmt)
346-
cli::cli_end()
347-
348-
invisible(x)
349-
}
350-
351-
# Currently only used in the workflow printing
352-
print_preprocessor_recipe <- function(x, ...) {
353-
recipe <- workflows::extract_preprocessor(x)
354-
steps <- recipe$steps
355-
n_steps <- length(steps)
356-
cli::cli_text("{n_steps} Recipe step{?s}.")
357-
358-
if (n_steps == 0L) {
359-
return(invisible(x))
360-
}
361-
362-
step_names <- map_chr(steps, workflows:::pull_step_name)
363-
364-
if (n_steps <= 10L) {
365-
cli::cli_ol(step_names)
366-
return(invisible(x))
367-
}
368-
369-
extra_steps <- n_steps - 10L
370-
step_names <- step_names[1:10]
371-
372-
cli::cli_ol(step_names)
373-
cli::cli_bullets("... and {extra_steps} more step{?s}.")
374-
invisible(x)
375-
}
376-
377-
print_preprocessor <- function(x) {
378-
has_preprocessor_formula <- workflows:::has_preprocessor_formula(x)
379-
has_preprocessor_recipe <- workflows:::has_preprocessor_recipe(x)
380-
has_preprocessor_variables <- workflows:::has_preprocessor_variables(x)
381-
382-
no_preprocessor <- !has_preprocessor_formula && !has_preprocessor_recipe &&
383-
!has_preprocessor_variables
384-
385-
if (no_preprocessor) {
386-
return(invisible(x))
387-
}
388-
389-
cli::cli_rule("Preprocessor")
390-
cli::cli_text("")
391-
392-
if (has_preprocessor_formula) {
393-
workflows:::print_preprocessor_formula(x)
394-
}
395-
if (has_preprocessor_recipe) {
396-
print_preprocessor_recipe(x)
397-
}
398-
if (has_preprocessor_variables) {
399-
workflows:::print_preprocessor_variables(x)
400-
}
290+
o <- cli::cli_fmt(NextMethod())
291+
# Fix up the recipe name
292+
rr <- unlist(strsplit(o[2], "Recipe"))
293+
len <- nchar(rr[2])
294+
h1_tail <- paste0(substr(rr[2], 1, len / 2 - 10), substr(rr[2], len / 2, len))
295+
o[2] <- paste0(rr[1], "Epi Recipe", h1_tail)
296+
297+
# Number the operations
298+
ops <- seq(grep(" Operations ", o, fixed = TRUE) + 1, length(o))
299+
rep_ops <- sub("\033[36m•\033[39m ", "", o[ops], fixed = TRUE) # kills the •
300+
o[ops] <- paste0(ops - ops[1] + 1, ". ", rep_ops)
301+
cli::cli_bullets(o)
401302
invisible(x)
402303
}

R/epi_workflow.R

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,13 @@
3232
#'
3333
#' wf
3434
epi_workflow <- function(preprocessor = NULL, spec = NULL, postprocessor = NULL) {
35-
out <- workflows::workflow(spec = spec)
36-
class(out) <- c("epi_workflow", class(out))
35+
out <- workflows::workflow(preprocessor = preprocessor, spec = spec)
3736

38-
if (is_epi_recipe(preprocessor)) {
39-
out <- add_epi_recipe(out, preprocessor)
40-
} else if (!is_null(preprocessor)) {
41-
out <- workflows:::add_preprocessor(out, preprocessor)
42-
}
4337
if (!is_null(postprocessor)) {
4438
out <- add_postprocessor(out, postprocessor)
4539
}
4640

41+
class(out) <- c("epi_workflow", class(out))
4742
out
4843
}
4944

@@ -162,11 +157,14 @@ predict.epi_workflow <- function(object, new_data, type = NULL, opts = list(), .
162157
}
163158
components <- list()
164159
components$mold <- workflows::extract_mold(object)
165-
components$forged <- hardhat::forge(new_data,
160+
components$forged <- hardhat::forge(
161+
new_data,
166162
blueprint = components$mold$blueprint
167163
)
168164
components$keys <- grab_forged_keys(components$forged, object, new_data)
169-
components <- apply_frosting(object, components, new_data, type = type, opts = opts, ...)
165+
components <- apply_frosting(
166+
object, components, new_data, type = type, opts = opts, ...
167+
)
170168
components$predictions
171169
}
172170

@@ -216,10 +214,7 @@ new_epi_workflow <- function(
216214

217215
#' @export
218216
print.epi_workflow <- function(x, ...) {
219-
print_header(x)
220-
print_preprocessor(x)
221-
# workflows:::print_case_weights(x)
222-
print_model(x)
217+
NextMethod()
223218
print_postprocessor(x)
224219
invisible(x)
225220
}

0 commit comments

Comments
 (0)