Skip to content
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

Confusing error when forgetting to pipe input (i.e., first arg isn't a dataframe) #6028

Closed
kcarnold opened this issue Sep 23, 2021 · 2 comments · Fixed by #6345
Closed

Confusing error when forgetting to pipe input (i.e., first arg isn't a dataframe) #6028

kcarnold opened this issue Sep 23, 2021 · 2 comments · Fixed by #6345
Labels
feature a feature request or enhancement rows ↕️ Operations on rows: filter(), slice(), arrange() vctrs ↗️

Comments

@kcarnold
Copy link

Students often forget pipes. I wonder if we could make clearer errors in that case. The error for arrange is particularly inscrutable:

library(tidyverse)
diamonds %>% 
  group_by(cut) %>% 
  summarize(n = n())
  arrange(desc(n))
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type 'closure'
#> Error in x[!nas]: object of type 'closure' is not subsettable

Created on 2021-09-23 by the reprex package (v2.0.1)

The other possible errors in this pipeline are also confusing (no applicable method for 'group_by' applied to an object of class "function" and Error: n() must only be used inside dplyr verbs.)

The errors in this particular pipeline could be improved somewhat by adding an informational group_by.default, tweaking the logic around the existing n() error, and some additional type-checking in desc. ggplot has some nice messaging like "Did you use %>% instead of +?".

Vaguely related: #4127

@romainfrancois
Copy link
Member

Maybe something like:

arrange <- function(.data, ..., .by_group = FALSE) {
  .data <- impose(.data, "arrange")
  UseMethod("arrange")
}

impose <- function(.data, .fn) {
  withCallingHandlers(.data, error = function(e) {
    abort(c(
      glue("Error when evaluating `{.fn}(.data=)`."),
      i = "This might to be a missing pipe."
    ), parent = e)
  })
}

This'd give something like this:

[master*] 226.48 MB > arrange(desc(n))
Error: 
  Error when evaluating `arrange(.data=)`.
  ℹ This might to be a missing pipe.
Caused by error in `[`: 
  object of type 'closure' is not subsettable
Run `rlang::last_error()` to see where the error occurred.
Warning message:
In is.na(x) : is.na() applied to non-(list or vector) of type 'closure'

but we did rule it out in #4127. Or perhaps this is a variant of UseMethod() that enforces object to be greedy ?

@hadley
Copy link
Member

hadley commented Apr 16, 2022

The specific error here is coming from xtfrm:

library(dplyr, warn.conflicts = FALSE)
xtfrm(n)
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type 'closure'
#> Error in x[!nas]: object of type 'closure' is not subsettable

Created on 2022-04-16 by the reprex package (v2.0.1)

We could fix this either by adding some additional type checks to desc() or by maybe switching from xtrfm() to vctrs::vec_proxy_order(), but it doesn't have quite the behaviour we're looking for either:

vctrs::vec_proxy_order(mean)
#> function (x, ...) 
#> UseMethod("mean")
#> <bytecode: 0x7fabf3671100>
#> <environment: namespace:base>

Created on 2022-04-16 by the reprex package (v2.0.1)

@hadley hadley added feature a feature request or enhancement vctrs ↗️ rows ↕️ Operations on rows: filter(), slice(), arrange() labels Apr 16, 2022
hadley added a commit that referenced this issue Jul 21, 2022
hadley added a commit that referenced this issue Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement rows ↕️ Operations on rows: filter(), slice(), arrange() vctrs ↗️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants