Skip to content

Suggestion for more informative error message when () missing from geom_* #3571

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
mine-cetinkaya-rundel opened this issue Oct 16, 2019 · 7 comments · Fixed by #3594
Closed

Comments

@mine-cetinkaya-rundel
Copy link
Member

If you omit the () at the end of a geom, you get the following error:

library(ggplot2)

ggplot(mtcars, aes(y = mpg, group = vs)) +
  geom_boxplot
#> Error: Don't know how to add geom_boxplot to a plot

I think the error would be more informative if it continued on to say "Did you mean geom_boxplot()?"

@yutannihilation
Copy link
Member

This has been discussed on #3106 (comment) and some of us actually agreed the error message can be improved.

@mine-cetinkaya-rundel
Copy link
Member Author

I see, thanks for the link!

This issue wasn't prompted by confusion between %>% and + or people using functions without () in pipelines. In most cases where I've seen code like this it wasn't due to an expectation of geom_point() working without the (), it was just forgetting to add the parantheses. I, like many others, start teaching with visualization/ggplot2 so there are lots of very newbie mistakes as people are learning the syntax.

I also agree that this is not widespread confusion, but more of a nice-to-have to help the few who struggle.

Note that I have not done a cost-benefit analysis of implementation difficulty vs. benefit of having the improved error in place, so I can't really say if it's "worth it" to make the change.

@yutannihilation
Copy link
Member

Ah, thanks for the context. I guess it won't be very difficult to add some heuristic here, which is called only when raising this error:

ggplot_add.default <- function(object, plot, object_name) {
stop("Don't know how to add ", object_name, " to a plot", call. = FALSE)
}

@clauswilke
Copy link
Member

Yes, just check whether object is a function, and if it is suggest that they may have forgotten the parentheses.

@clauswilke
Copy link
Member

Actually, I think this will do it. I can prepare a PR.

library(ggplot2)

# ggplot_add variant to be added
ggplot_add.function <- function(object, plot, object_name) { 
  stop(
    "Don't know how to add ", object_name, " to a plot. Did you mean ",
    object_name, "()?", call. = FALSE
  ) 
} 

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

Created on 2019-10-28 by the reprex package (v0.3.0)

@clauswilke
Copy link
Member

See #3594.

@lock
Copy link

lock bot commented May 5, 2020

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 May 5, 2020
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 a pull request may close this issue.

3 participants