tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.75k stars 2.12k forks source link

Proper syntax for character vector quasi-quotation #2697

Closed mjsteinbaugh closed 7 years ago

mjsteinbaugh commented 7 years ago

Hi all, I'm reading through the new dplyr programming vignette and am working on updating the syntax used in my packages. I'm confused how to properly pass in character vectors (e.g. for select(), arrange()) with the new tidyeval methods using standard evaluation. What's the recommended syntax for quasi-quotation of a character vector for column name matching in place of the now deprecated .dots method? Everything else is working great for me in the latest release candidate.

head(starwars)
cols <- c("name", "height", "mass")

# `select()` examples ====
# Deprecated dplyr 0.5.0 method
select_(starwars, .dots = cols)

# Works, but with integer positions
quosure <- quo(1:3)
select(starwars, !!quosure)

# Works, but only for single name
quosure <- quo(name)
select(starwars, !!quosure)

# Works, but is there an unquote approach like above?
select(starwars, one_of(cols))

# `arrange()` examples ====
# Deprecated dplyr 0.5.0 method
arrange_(starwars, .dots = cols)
# Current best practice SE method for dplyr 0.6.0?

Thanks, Mike

lionel- commented 7 years ago

this would be !!! rlang::syms(chr_vector). @hadley should sym() and syms() be reexported in dplyr?

mjsteinbaugh commented 7 years ago

Awesome, works great. I was close in the rlang documentation!

select(starwars, !!!rlang::syms(cols))
arrange(starwars, !!!rlang::syms(cols))
hadley commented 7 years ago

I don't think we need to re-export it because you're using the

MikeBadescu commented 7 years ago

For completion, is there other solution than using !!! rlang::syms(chr_vector)?

lionel- commented 7 years ago

syms(x) is equivalent to lapply(x, as.name)

MikeBadescu commented 7 years ago

Lionel, thank you! In general, it would help if SE would be first class throughout in tidyverse.