tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.77k stars 2.12k forks source link

Should dplyr commands gobble empty last argument? #1039

Closed rpruim closed 7 years ago

rpruim commented 9 years ago

It would be nice to allow a trailing comma in commands like the example below. That would make it easier to rearrange the order or to comment out lines without needing to figure out whether the commas need adjusting.

diamonds %>%
  group_by(color, clarity) %>%
  summarise(
    depth = mean(depth),
    price = mean(price),
    carat = mean(carat),  #  <-- trailing comma here currently causes errors
  )

I'm imaging a function that could be called at the top of a function definition that would gobble this empty argument. Or perhaps a wrapper function a la Vectorize() that could be used to convert any function into a function with this behavior. Seems like this could be useful for many functions both within dplyr and elsewhere.

romainfrancois commented 9 years ago

Hmm, I don't think I would like that. dplyr is still R code.

rpruim commented 9 years ago

I don't see how this would violate being R code. Is it substantially different from

x[2, ]

which is standard R usage?

Of course, you would be free not to use this feature.

romainfrancois commented 9 years ago

well x[2, ] is not the same as x[2]

rpruim commented 9 years ago

True. But it is an example where an empty argument doesn't cause things to break and is useful. So I think there is no prohibition on writing functions that allow empty arguments.

I have often wished that c() and list() had this behavior. It would make code maintenance easier.

hadley commented 9 years ago

I see this as a small violation of R's usual rules, in the service of making a common error less common. I don't see any major downsides (except that people might come to expect this in all R functions).

I remember when this wasn't an error in R, and I still miss those days :(

romainfrancois commented 9 years ago

In that case, we are probably looking at a change in lazy_dots or perhaps how we use it here.

hadley commented 7 years ago

@lionel- can you take this on as part of tidyeval conversion?