Skip to content

Conversation

@EmilHvitfeldt
Copy link
Member

to close #887

Adds ignore_step(), with a similar interface as tidy() with respect to how we select, either using number or id.

I'm not in love with the name, so suggestions are welcome.

Copy link
Member

@hfrick hfrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not use "ignore" in the name here because that implies the step(s) in question are still part of the recipe, they just don't get used -- but this does remove them. I think remove_step() would be the cleanest but if we want to avoid "remove" because of step_rm(), we could go with "undo" or "delete".

Comment on lines +40 to +44
if (n_steps == 0) {
cli::cli_abort("{.arg x} doesn't contain any steps to remove.")
}

arg <- rlang::check_exclusive(number, id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could allow ignore_step(<recipes without steps>) and just have it do nothing. If we don't want that, I would swap these two checks. To me, it's more obvious then that we don't allow that because we always require you to provide what to remove (before we tell you that there is nothing to remove).

#'
#' ignore_step(rec, id = "PCA")
#' @export
ignore_step <- function(x, number, id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would give number and id some default values since only one of the two is required. tidy() uses NA.

Comment on lines +24 to +28
expect_equal(
ignore_attr = TRUE,
ignore_step(rec1234, number = 1),
rec234
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised by the argument order here, why do you put ignore_attr up front?

#' `ignore_step` will return a recipe without steps specified by the `number` or
#' `id` argument.
#'
#' @param x A `recipe` object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#' @param x A `recipe` object.
#' @param x An untrained `recipe` object.

Comment on lines +7 to +10
#' @param number An integer vector, Denoting the positions of the steps that
#' should be removed.
#' @param id A character string. Denoting the `id` of the steps that should be
#' removed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#' @param number An integer vector, Denoting the positions of the steps that
#' should be removed.
#' @param id A character string. Denoting the `id` of the steps that should be
#' removed.
#' @param number An integer vector denoting the positions of the steps that
#' should be removed.
#' @param id A character string denoting the `id` of the steps that should be
#' removed.

@EmilHvitfeldt
Copy link
Member Author

We are sitting on this for another week. Brief summary of meeting discussion:

  • There is some doubt that this could lead to problems down the line AND/OR subtle bugs
  • feels like syntactic sugar
  • Isn't a bad idea, but maybe better suited for an extension package
  • can be imagined as part of a broader group of functions that modify recipes
    • swap steps, move steps up and down

@EmilHvitfeldt
Copy link
Member Author

we are sitting on this at least another week

@sen-kaustav
Copy link

Hello,

I stumbled onto this feature when trying to figure out a way to modify my tidymodels recipe in my Shiny app.

For context, I am building a Shiny app to allow end users to interactively fit and analyse GLM models. As part of this, I am storing my recipe() as a reactiveVal() allowing it to be modified as the user interacts with the UI. In this setup, being able to programmatically "reset" some parts of the recipe is really beneficial from a user experience perspective. This allows them to go back and forth with trying out different pre-processing steps like how you would do in a coding / scripting environment.

As a workaround, I have currently copied over the ignore_step.R file to my project to be able to use this functionality. I have also modified it slightly to allow the passing a recipe step name as a character string. For example, the below will delete the step_normalize() step from the recipe:

rec <- recipe(mpg ~ ., data = mtcars) %>%
  step_dummy(all_nominal_predictors()) %>%
  step_impute_mean(all_numeric_predictors()) %>%
  step_normalize(all_numeric_predictors()) %>%
  step_pca(all_numeric_predictors(), id = "PCA")

ignore_step(rec, step_type = "step_normalize")

For reference, here's a link to the modified ignore_step.R file that I am using: ignore_step.R

However, reading through the previous comments I can see that there are still a few open design considerations. Given that here hasn't been any further traction on this ticket, I am guessing there are no plans to include this in a future release? The current workaround does the job for me but having it as part of the core recipes package (or an extension package) would definitely be ideal!

Thanks again for the original code! It really helped me solve my problem 😄.

P.S. I am also not a huge fan of the ignore_step() name. omit_step() or delete_step() (as previously suggested) has a much better ring to it.

@EmilHvitfeldt
Copy link
Member Author

Hello @sen-kaustav 👋

I'm happy to hear that you found the function useful, and you are of course welcome to have copied the function over to your project. After having been thinking it over, I don't think that this function should belong in {recipes} itself. It is definitely useful but we don't think it fits the scope of this package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Function that removes step(s) from an existing recipe

4 participants