Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Djm/update mods #258

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions R/epi_recipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -404,19 +404,23 @@ adjust_epi_recipe.epi_recipe <- function(
x$steps[[which_step]] <- update(x$steps[[which_step]], ...)
} else {
step_names <- map_chr(x$steps, ~ attr(.x, "class")[1])
starts_with_step <- substr(which_step, 1, 5) == "step_"
if (!starts_with_step) which_step <- paste0("step_", which_step)

if (!(which_step %in% step_names)) {
cli::cli_abort(
c("`which_step` is not in the `epi_recipe` step names. ",
"i" = "The step names are {step_names}."
)
)
cli::cli_abort(c(
"`which_step` does not appear in the available `epi_recipe` step names. ",
i = "The step names are {.val {step_names}}."
))
}
which_step_idx <- which(step_names == which_step)
if (length(which_step_idx) == 1) {
x$steps[[which_step_idx]] <- update(x$steps[[which_step_idx]], ...)
} else {
cli::cli_abort("`which_step` is not unique. Matches steps: {which_step_idx}.")
cli::cli_abort(c(
"`which_step` is not unique. Matches steps: {.val {which_step_idx}}.",
i = "Please use the step number instead for precise alterations."
))
}
}
x
Expand Down Expand Up @@ -444,7 +448,7 @@ prep.epi_recipe <- function(
}
skippers <- map_lgl(x$steps, recipes:::is_skipable)
if (any(skippers) & !retain) {
rlang::warn(c(
cli::cli_warn(c(
"Since some operations have `skip = TRUE`, using ",
"`retain = TRUE` will allow those steps results to ",
"be accessible."
Expand Down Expand Up @@ -477,7 +481,7 @@ prep.epi_recipe <- function(
)
training <- bake(x$steps[[i]], new_data = training)
if (!tibble::is_tibble(training)) {
abort("bake() methods should always return tibbles")
cli::cli_abort("`bake()` methods should always return {.cls tibble}.")
}
if (!is_epi_df(training)) {
# tidymodels killed our class
Expand Down
39 changes: 22 additions & 17 deletions R/frosting.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,23 @@ adjust_frosting.frosting <- function(
x$layers[[which_layer]] <- update(x$layers[[which_layer]], ...)
} else {
layer_names <- map_chr(x$layers, ~ attr(.x, "class")[1])
starts_with_layer <- substr(which_layer, 1, 6) == "layer_"
if (!starts_with_layer) which_layer <- paste0("layer_", which_layer)

if (!(which_layer %in% layer_names)) {
cli::cli_abort(
c("`which_layer` is not in the `frosting` layer names. ",
"i" = "The layer names are {layer_names}."
)
)
cli::cli_abort(c(
"`which_layer` does not appear in the available `frosting` layer names. ",
i = "The layer names are {.val {layer_names}}."
))
}
which_layer_idx <- which(layer_names == which_layer)
if (length(which_layer_idx) == 1) {
x$layers[[which_layer_idx]] <- update(x$layers[[which_layer_idx]], ...)
} else {
cli::cli_abort("`which_layer` is not unique. Matches layers: {which_layer_idx}.")
cli::cli_abort(c(
"`which_layer` is not unique. Matches layers: {.val {which_layer_idx}}.",
i = "Please use the layer number instead for precise alterations."
))
}
}
x
Expand All @@ -212,7 +216,7 @@ add_postprocessor <- function(x, postprocessor, ..., call = caller_env()) {
if (is_frosting(postprocessor)) {
return(add_frosting(x, postprocessor))
}
rlang::abort("`postprocessor` must be a frosting object.", call = call)
cli::cli_abort("`postprocessor` must be a frosting object.", call = call)
}

is_frosting <- function(x) {
Expand All @@ -223,8 +227,8 @@ is_frosting <- function(x) {
validate_frosting <- function(x, ..., arg = "`x`", call = caller_env()) {
rlang::check_dots_empty()
if (!is_frosting(x)) {
glubort(
"{arg} must be a frosting postprocessor, not a {class(x)[[1]]}.",
cli::cli_abort(
"{arg} must be a frosting postprocessor, not a {.cls {class(x)[[1]]}}.",
.call = call
)
}
Expand Down Expand Up @@ -283,10 +287,9 @@ new_frosting <- function() {
#' p
frosting <- function(layers = NULL, requirements = NULL) {
if (!is_null(layers) || !is_null(requirements)) {
rlang::abort(c(
"Currently, no arguments to `frosting()` are allowed",
"to be non-null."
))
cli::cli_abort(
"Currently, no arguments to `frosting()` are allowed to be non-null."
)
}
out <- new_frosting()
}
Expand All @@ -305,7 +308,8 @@ extract_frosting <- function(x, ...) {

#' @export
extract_frosting.default <- function(x, ...) {
abort(c("Frosting is only available for epi_workflows currently.",
cli::cli_abort(c(
"Frosting is only available for epi_workflows currently.",
i = "Can you use `epi_workflow()` instead of `workflow()`?"
))
invisible(x)
Expand Down Expand Up @@ -339,7 +343,8 @@ apply_frosting <- function(workflow, ...) {
#' @export
apply_frosting.default <- function(workflow, components, ...) {
if (has_postprocessor(workflow)) {
abort(c("Postprocessing is only available for epi_workflows currently.",
cli::cli_abort(c(
"Postprocessing is only available for epi_workflows currently.",
i = "Can you use `epi_workflow()` instead of `workflow()`?"
))
}
Expand Down Expand Up @@ -367,8 +372,8 @@ apply_frosting.epi_workflow <-
}

if (!has_postprocessor_frosting(workflow)) {
rlang::warn(c(
"Only postprocessors of class frosting are allowed.",
cli::cli_warn(c(
"Only postprocessors of class {.cls frosting} are allowed.",
"Returning unpostprocessed predictions."
))
components$predictions <- predict(
Expand Down
8 changes: 2 additions & 6 deletions R/tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,9 @@ tidy.frosting <- function(x, number = NA, id = NA, ...) {
res
}

#' @rdname tidy.layer
#' @export
tidy.layer <- function(x, ...) {
rlang::abort(
paste0(
"No `tidy` method for a layer with classes: ",
paste0(class(x), collapse = ", ")
)
cli::cli_abort(
"No `tidy()` method exists for a layer with class: {.cls {class(x)}}."
)
}
3 changes: 3 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ reference:
- epi_recipe
- epi_workflow
- add_epi_recipe
- adjust_epi_recipe
- add_model
- predict.epi_workflow
- fit.epi_workflow
- augment.epi_workflow
Expand All @@ -62,6 +64,7 @@ reference:
- frosting
- ends_with("_frosting")
- get_test_data
- tidy.frosting
- title: Frosting layers
contents:
- contains("layer")
Expand Down
Empty file removed add_model.Rd
Empty file.