Skip to content

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

Closed

Description

@mine-cetinkaya-rundel

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()?"

Activity

  1. yutannihilation commented on Oct 28, 2019

    @yutannihilation
    Member

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

  2. mine-cetinkaya-rundel commented on Oct 28, 2019

    @mine-cetinkaya-rundel
    MemberAuthor

    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.

  3. yutannihilation commented on Oct 28, 2019

    @yutannihilation
    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)
    }

  4. clauswilke commented on Oct 28, 2019

    @clauswilke
    Member

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

  5. clauswilke commented on Oct 28, 2019

    @clauswilke
    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)

  6. added a commit that references this issue on Oct 28, 2019
    277074d
  7. clauswilke commented on Oct 28, 2019

    @clauswilke
    Member

    See #3594.

  8. added a commit that references this issue on Oct 29, 2019
    5188f5e
  9. lock commented on May 5, 2020

    @lock

    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/

  10. locked and limited conversation to collaborators on May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions