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

Q: Data first in the pipeline? #46

Closed
njtierney opened this issue Apr 11, 2017 · 5 comments
Closed

Q: Data first in the pipeline? #46

njtierney opened this issue Apr 11, 2017 · 5 comments

Comments

@njtierney
Copy link

@njtierney njtierney commented Apr 11, 2017

Hi there!

Thanks so much for your work so far on this, I'm really looking forward to seeing this package develop!

I was wondering if you have any thoughts on placing data as the first argument in a recipe?

This would then mean that the README code could then be:

sonar_rec <- Sonar %>%
  recipe(Class ~ .) %>%
  step_center(all_predictors()) %>%
  step_scale(all_predictors())

This idea has been made possible with the intubate package, but perhaps this could be something provided in recipes?

Thank you again for all your work!

@topepo
Copy link
Collaborator

@topepo topepo commented Apr 11, 2017

The formula comes first so that that it is an S3 formula method for the recipe class. Let me see if there is a fancy way to do this without resorting to S4 methods.

@njtierney
Copy link
Author

@njtierney njtierney commented Apr 13, 2017

OK, sounds good! :)

@topepo
Copy link
Collaborator

@topepo topepo commented Apr 14, 2017

Some thinking quasi out loud about this...

If we were to do this, we would get rid of the formula method for the recipe class and use the default method to direct arguments to places that they should go.

The current class system has:

recipe <- function(x, ...) UseMethod("recipe")

## `...`` are not currently hooked up to anything

recipe.default <- function(x, vars = colnames(x), roles = NULL, ...) {
  # code
}

recipe.formula <- function(formula, data, ...) {
  # code that eventually calls `recipe.default`
}

If the function is called in the usual way with a formula:

recipe(y ~ x, data = biomass_tr)

but there is no formula method, this ends up being

# case 1
recipe.default(x = y ~ x, data = biomass_tr)

Note that data is now part of ... and would need to be intercepted and reassigned.

If called from a pipe:

data %>% recipe(y ~ x)

the default method would result in

# case 2
recipe.default(x = biomass_tr, vars = y ~ x)

In both cases, we would need to pass to some other non-method function (say recipe_form) that executes the workflow that the current recipe.formula has and then passes arguments to recipe.default.

It seems pretty ugly since, in case 1, x is nothing like what we would claim in the man page. I could change it to something like object but that is pretty kludgy since I don't think that anyone would expect something called object to be the data (in R, anyway).

Is there a more elegant way to get to the same place?

@hadley
Copy link
Collaborator

@hadley hadley commented Apr 14, 2017

Why not something like this:

recipe <- function(data, formula = NULL, ...) UseMethod("recipe")

recipe.data.frame <- function(x, formula = NULL, ..., vars = colnames(x), roles = NULL) {
  
  recipe <- ...

  if (!is.null(formula))
    recipe <- step_formula(recipe, formula)
  
}

Then recipe(data, formula) is just a convenient short hand for data %>% recipe() %>% step_formula(formula)

topepo added a commit that referenced this issue Apr 15, 2017
@topepo
Copy link
Collaborator

@topepo topepo commented Apr 15, 2017

That should solve it but please test with your problem

@topepo topepo closed this Apr 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.