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

string coercion affects c() #37

Closed
gergness opened this issue Sep 5, 2017 · 2 comments
Closed

string coercion affects c() #37

gergness opened this issue Sep 5, 2017 · 2 comments

Comments

@gergness
Copy link
Contributor

gergness commented Sep 5, 2017

Was testing things out after reading Hadley's blog post, and found this edge case:

When using c(), we still get errors when combining string names and column numbers. Is it possible to work around this, as in :?

library(tidyselect)

vars_select(names(mtcars), c(starts_with("m"), "cyl"))
#> Error: Strings must match column names. Unknown columns: 1


vars_select(names(mtcars), starts_with("m") : "cyl")
#>   mpg   cyl 
#> "mpg" "cyl" 

If you can't fix it, the blog post is a little inconsistent about whether this will work. The paragraph after the code block implies it will work for c()

Luckily tidyselect 0.2.0 also introduces a few features that help writing safer code for data expressions. First, the support for strings and character vectors has been improved. All data expressions fully support strings. It is now valid to supply strings to - and ::
...
Note that this only applies to c(), - or : because it would not make sense to write seq("name", "mass")

@lionel-
Copy link
Member

lionel- commented Sep 6, 2017

Note that c(starts_with("m"), "cyl") is equivalent to supplying the arguments separately.

And starts_with("m") : "cyl" is unsafe because starts_with() might return a vector of several elements.

@gergness
Copy link
Contributor Author

gergness commented Sep 6, 2017

That's true within tidyverse packages, but I have been using c() for srvyr, in functions structured like mutate_at(), as an alternative to requiring users to use vars() around the variables. (a little more discussion here: tidyverse/dplyr#2685)

And yes, that example isn't great, I just wanted to show that things that work with : don't always work with c(). I think these might actually come up in real world usage:

vars_select(names(mtcars), c(mpg, "cyl"))
#> Error: Strings must match column names. Unknown columns: 1

vars_select(names(mtcars), mpg : "cyl")
#>   mpg   cyl 
#> "mpg" "cyl"


my_var <- "cyl"
vars_select(names(mtcars), c(mpg, !!my_var))
#> Error: Strings must match column names. Unknown columns: 1

vars_select(names(mtcars), mpg : !!my_var)
#>   mpg   cyl 
#> "mpg" "cyl"

@lionel- lionel- closed this as completed in 81bbab4 Oct 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants