r-lib / tidyselect Public
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 |
@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: