-
Notifications
You must be signed in to change notification settings - Fork 39
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
Lookup ambiguity in data-expressions #76
Comments
@romainfrancois commented on Sep 24, 2018, 7:35 AM UTC: This is the documented behavior. Columns have priority in This is in essence, |
This was previously discussed in #49, with the resolution to keep the behavior. But that issue didn't include the example here: library(tidyselect)
# Dangerous: two way to interpret
x <- "y"
vars_select("x", x)
#> x
#> "x"
vars_select("x", x:y)
#> Error in is_character(x, encoding = encoding, n = 1L): object 'y' not found
vars_select("x", -x)
#> named character(0)
vars_select("y", x)
#> y
#> "y"
vars_select("y", x:y)
#> y
#> "y"
vars_select("y", -x)
#> named character(0)
# Safe
vars_select("x", !!x)
#> Error: Unknown column `y`
#> Backtrace:
#> ─tryCatch(...)
#> ─vars_select("x", !!x)
#> ─vars_select_eval(.vars, quos) /home/kirill/git/R/tidyselect/R/vars-select.R:118:2
#> ─map_if(ind_list, is_character, match_strings, names = TRUE) /home/kirill/git/R/tidyselect/R/vars-select.R:236:2
#> ─map(.x[sel], .f, ...) /tmp/RtmpqZVXSG/R.INSTALL62801d434702/purrr/R/map.R:112:2
#> ─.f(.x[[i]], ...) /tmp/RtmpqZVXSG/R.INSTALL62801d434702/purrr/R/map.R:104:2
#> ─bad_unknown_vars(vars, unknown) /home/kirill/git/R/tidyselect/R/vars-select.R:272:4
vars_select("x", (!!x):y)
#> Error: Unknown column `y`
#> Backtrace:
#> ─tryCatch(...)
#> ─"y":y
#> ─match_strings(x) /home/kirill/git/R/tidyselect/R/vars-select.R:243:4
#> ─bad_unknown_vars(vars, unknown) /home/kirill/git/R/tidyselect/R/vars-select.R:272:4
vars_select("x", -!!x)
#> Error: Unknown column `y`
#> Backtrace:
#> ─tryCatch(...)
#> ─-"y"
#> ─match_strings(x) /home/kirill/git/R/tidyselect/R/vars-select.R:257:4
#> ─bad_unknown_vars(vars, unknown) /home/kirill/git/R/tidyselect/R/vars-select.R:272:4
vars_select("y", !!x)
#> y
#> "y"
vars_select("y", (!!x):y)
#> y
#> "y"
vars_select("y", -!!x)
#> named character(0)
# Correct (but why is the error message misleading?)
x <- rlang::sym("y")
vars_select("x", !!x)
#> Error in .f(.x[[i]], ...): object 'y' not found
vars_select("x", (!!x):y)
#> Error in is_character(x, encoding = encoding, n = 1L): object 'y' not found
vars_select("x", -!!x)
#> Error in is_character(x): object 'y' not found
vars_select("y", !!x)
#> y
#> "y"
vars_select("y", (!!x):y)
#> y
#> "y"
vars_select("y", -!!x)
#> named character(0) Created on 2018-09-24 by the reprex package (v0.2.1.9000) For now the easiest remedy might be to give an appropriate warning in the documentation, and refer to |
This type of variable lookup in selection contexts now triggers a message: vars <- c("cyl", "disp")
mtcars %>% dplyr::select(vars) %>% head()
#> Note: Selecting non-column variables is brittle.
#> ℹ If the data contains `vars` it will be selected instead.
#> ℹ Use `all_of(vars)` to silence this message.
#> cyl disp
#> Mazda RX4 6 160
#> Mazda RX4 Wag 6 160
#> Datsun 710 4 108
#> Hornet 4 Drive 6 258
#> Hornet Sportabout 8 360
#> Valiant 6 225 This is the first step towards deprecation of this behaviour. |
I think I found an odd case that is part of this issue. Piping a selection helper into
The last line recommends I do not know if this is an issue with the code that generates the message or with my understanding of the pipe operator. I think most users understand dplyr version: 1.0.6 |
This is more or less how |
Is there any way to identify when the user has used |
hmm not really. The check would have to be implemented at the level of |
While this combinations of behaviours certainly has a couple of somewhat surprising edge cases, we believe that the individual behaviours are sound, and can't see an obvious way to avoid the problem that arises in their combination. So our plan is to leave tidyselect semantics as they currently are, but we'll certainly revisit if we discover that these problems are more widespread than we initially thought. |
The warning message below mentions dplyr::select and the FAQ mentions also tidyr:::pivot_longer. I have came across the same message with tidyr::unite. I am guessing it is all the functions that can use an Argument type: tidy-select. Would it help to update the FAQ to not only name select and pivot_longer
|
This is an old issue, but I'm still not managing to find good documentation with example for how to resolve other sources of this warning. I seem to be getting it when using I'm worried that in a future version the warning will become an error, but am confused about how to use external vectors/variables in methods other than select and pivot_longer |
Ran into this old closed issue today with similar concerns as @yfarjoun above. Some additional documentation I found that helped me to resolve the
Embracing the function argument in the reprex:
Generate the warning by using either function, e.g.
Btw, the warning seems to get suppressed after it appears once in an R session. So if you try both functions above in the same R session, the second one you try might seem to actually not generate a warning. Refresh your R session and try again to confirm it's actually still generating it. That dplyr_tidy_select help page says we should embrace
Try it out
I failed a bunch of times trying to resolve the warning for functions that listed the variables in the function argument instead of listing them in a character vector in the function argument, e.g. functions like
The first modified The correct way to modify the select statement is
Some other modified
Session infoR 4.2.2 R 4.3.1 |
@JohnMount commented on Sep 23, 2018, 2:35 PM UTC:
dplyr::select()
returns a wrong column, should probably throw a "no such column/value" style exception.This issue was moved by romainfrancois from tidyverse/dplyr#3851.
The text was updated successfully, but these errors were encountered: