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

do() does not work with inlined function definitions #574

Closed
holgerbrandl opened this issue Sep 3, 2014 · 5 comments
Closed

do() does not work with inlined function definitions #574

holgerbrandl opened this issue Sep 3, 2014 · 5 comments
Assignees
Labels
feature a feature request or enhancement
Milestone

Comments

@holgerbrandl
Copy link

Using todays github-snapshot this works

identity <-function(x) x
iris %>% group_by(Species) %>% do(identity(.))

but the the same logic with the method definition being inlined fails without any obvious reason

iris %>% group_by(Species) %>% do(function(x){ x }(.))
> Error: Results are not data frames at positions: 1, 2, 3

It looks like it worked a while ago (see http://stackoverflow.com/a/22182914/590437)

Adding another set of round brackets around the function definition seems to solve the problem, but I doubt that many users will guess this:

iris %>% group_by(Species) %>% do((function(x){ x })(.))

For improved readability it would be great if (.) could be optionally left out. Then function object would be passed as argument to do which would need to invoke it with the the group table as sole argument. This was how it worked beautifully with ddply.

@kevinykuo
Copy link

@holgerbrandl you should really enclose anonymous functions in parentheses.

possibly related: tidyverse/magrittr#12

@hadley
Copy link
Member

hadley commented Sep 3, 2014

The problem is that function(x){ x }(.) parses as function(x)({ x }(.)), not (function(x){ x })(.). I don't think this has changed recently (that SO question is the older do() interface).

The whole point of the . pronoun is to avoid defining anonymous functions. Maybe what you're asking for is for this to work?

iris %>% group_by(Species) %>% do(identity)

@holgerbrandl
Copy link
Author

@hadley: Indeed that's what I would love to do! It's the equivalent of ddply(iris, .(Species), identity). However, it does not work with the current version of dplyr:

iris %>% group_by(Species) %>% do(identity)
> Error: Results are not data frames at positions: 1, 2, 3

@KyKuo: I just call them right away with (.) because I was under the impression that this is the only way to go when using do(). It looks odd to me too.

Personally, I think that anonymous functions should be possible because when using do(). They allow for an optimal reading of code without jumping around. E.g

data %>% mutate(...) %>% group_by(...) %>% do(function(x){ ... really fancy stuff ..}) %>% select ...

compared to

never_reused_fun_with_odd_name <- function(x){ ... really fancy stuff ..}
data %>% mutate(...) %>% group_by(...) %>% do(never_reused_fun_with_odd_name(.)) %>% select ...

@hadley
Copy link
Member

hadley commented Sep 3, 2014

@holgerbrandl but you don't need an anonymous function to do that:

data %>% mutate(...) %>% group_by(...) %>% do({
  ... really fancy stuff ...
}) %>% select ...

@holgerbrandl
Copy link
Author

@hadley That's pretty cool and indeed makes anonymous function unnecessary here. Thank you very much.

@hadley hadley added the feature a feature request or enhancement label Sep 11, 2014
@hadley hadley added this to the 0.3.1 milestone Sep 11, 2014
@hadley hadley self-assigned this Sep 11, 2014
@hadley hadley closed this as completed Nov 18, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jun 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants