Skip to content

Better error message when forgetting to provide data to functions like mutate or summarize #7704

@dmuenz

Description

@dmuenz

I had a bug in my code where I'd forgotten to pass a data frame to summarize, and the resulting error message led me in the wrong direction. Here's an example:

dplyr::summarize(n = dplyr::n())
#> Error in `dplyr::n()`:
#> ! Must only be used inside data-masking verbs like `mutate()`, `filter()`, and `group_by()`.

Even though the bug here is that I forgot to specify the .data argument in summarize(), with that error message I thought I was somehow using n() incorrectly.

Note that the problem seems in part due to n() being a zero-argument function. Since it doesn't take any variables, it can't refer to any variables when erroring. In contrast, the following code gives a more helpful error message:

dplyr::summarize(mean = mean(x))
#> Error: object 'x' not found

Could the error message in the first example be improved? More generally, could dplyr functions check that .data is specified and give an appropriate error message if not?

I'm using dplyr 1.1.4 with R 4.4.1.

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