Dplyr difference between select and group_by relative to quoted variables?

In the current version of dplyr, arguments selectcan be passed by value:

variable <- "Species"
iris %>% 
    select(variable)

#       Species
#1       setosa
#2       setosa
#3       setosa
#4       setosa
#5       setosa
#6       setosa
#...

But arguments group_bycannot be passed by value:

iris %>% 
    group_by(variable) %>% 
    summarise(Petal.Length = mean(Petal.Length))

# Error in grouped_df_impl(data, unname(vars), drop) : 
# Column `variable` is unknown

dplyr :: select documented behavior

iris %>% select(Species)

And the documented documented behavior of dplyr :: group_by -

iris %>% 
    group_by(Species) %>% 
    summarise(Petal.Length = mean(Petal.Length))
  • Why selectand group_bydifferent with respect to the transfer of arguments by value?
  • Why selectdoes the first call work and will it continue to work in the future?
  • Why group_bydoes the first call not work? I am trying to figure out which combination quo(), enquo()and !!I have to use to make it work.

, , , , .

+6
1

, quosure. sym parse_expr rlang , !! unquote:

library(dplyr)

variable <- rlang::sym("Species")
# variable <- rlang::parse_expr("Species")

iris %>% 
  group_by(!! variable) %>% 
  summarise(Petal.Length = mean(Petal.Length))

!! UQ(), . variable , , group_by.

sym parse_expr , ?

: .

:

- R, "" . , sym as.name R. parse_expr, , R-. parse R.

R-, , R-. , , R, - sym, , , .

, sym, ( parse_expr ) parse_expr, R .

variable , sym . , , , group_by !!.

+3

All Articles