Skip to content

Add default add to functions method #3106

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

Closed
wants to merge 1 commit into from
Closed

Add default add to functions method #3106

wants to merge 1 commit into from

Conversation

JohnMount
Copy link

Add ggplot_add.function() to plot-construction.r. This allows examples such as "ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point". This follows the pattern in magrittr of allowing both "5 %>% sin" and "5 %>% sin()". This increased regularity may be helpful for new users. New example worked and tests passed after change.

…s such as "ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point". This follows the pattern in magrittr of allowing both "5 %>% sin" and "5 %>% sin()". This increased regularity may be helpful for new users. New example worked and tests passed after change.
@yutannihilation
Copy link
Member

Thanks for the idea. But, + and %>% have very different semantics.

%>% forwards an object to a function (e.g. sin) or function call (e.g. sin()). Since what %>% does is to modify an expression and construct a call, it matters little whether it's a function or a function call (until you get supprised that 5 %>% base::sin doesn't work...)

On the other hand, + just adds the evaluated results of both sides. It doesn't modify the expression. So, I think a function (e.g. geom_point) and the result of a function call (e.g. result of geom_point()) are different things here.

I don't strongly disagree with this PR, but we need some discussion about the semantics, at least.

@JohnMount
Copy link
Author

JohnMount commented Jan 27, 2019

I just thought I would put it out there. Not my decision, but it does have some principles behind it.

Part of what inspired it was that students attempt to generalize from one to the other (and often both pipes an ggplot2 compositing are taught in the same course). I feel student experimentation should be rewarded, not punished. Plus the error message is confusing to the newcomer.

library(ggplot2)

ggplot(mtcars, aes(x=cyl, y=disp)) + geom_point
#> Error: Don't know how to add geom_point to a plot

Created on 2019-01-27 by the reprex package (v0.2.1)

An error message that mentioned the type or class of the thing it could not add might be clearer than mentioning just the name used.

There is a related semantic principle, I was just trying to be succinct in submitting the PR. The following code works:

library("ggplot2")

a <- geom_point()
ggplot(mtcars, aes(x=cyl, y=disp)) + a

Created on 2019-01-27 by the reprex package (v0.2.1)

Notice it doesn't say "Error: Don't know how to add a to a plot". The effect comes from R's evaluation semantics, but to the outside user it looks related: a substitution from name to the value was performed. For the function evaluation the principle is related: we can think of the function as an object needed a de-reference, and in ggplot2's world the natural de-reference may be evaluation (with no arguments).

There is some precedent for this sort of treatment. Many functional languages use zero-argument functions as "thunks" to control if/when evaluation happens. So this also represents a valid user controlled lazy evaluation opportunity.

@thomasp85
Copy link
Member

I can’t really figure out whether I like this idea or not. I cannot come up with any strong arguments against, but on the other hand I generally am against making piping and adding more alike as I think it will lead to greater confusion in the end...

If the error is confusing we should def tackle that, but I’d like to hear from @hadley, @karawoo, and @clauswilke about this PR before we decide a course of action

@clauswilke
Copy link
Member

Same here. I can't point to any reason why it's obviously problematic, but my gut feeling says no. I'm not sure it'll help students if they think geom_point and geom_point() are the same thing.

@clauswilke
Copy link
Member

However, I agree that the error message is a problem. If the error message was the following, would that help?

#> Error: Don't know how to add geom_point to a plot. Did you mean geom_point()?

@karawoo
Copy link
Member

karawoo commented Jan 27, 2019

I too think that blurring the distinction between %>% and + is likely to cause confusion of its own, but agree that the error message should be improved at the very least.

@hadley
Copy link
Member

hadley commented Jan 27, 2019

I definitely don’t think it’s a good idea. A better error might be nice, but it’s not something I’ve seen widespread confusion with.

@JohnMount JohnMount closed this Jan 28, 2019
@yutannihilation
Copy link
Member

I feel Claus's suggestion is compact enough, if we are to implement a better error.

Did you mean geom_point()?

Let this issue closed for now (until the need arises), in the hope that someday people will get unfamiliar with using %>% without (), which the tidyverse style doesn't recommend.

@lock
Copy link

lock bot commented Jul 28, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jul 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants