为什么表达式的 LHS 中的 rlang::sym 和 rlang::quo_name 表现相似?

Why do `rlang::sym` and `rlang::quo_name` in LHS of expression behave similarly?

使用foo(c("b"))调用下面的函数。输出以内联方式显示。

我很困惑为什么 (1) df %>% mutate(!!x_ := 100 + !!x)) 和 (2) df %>% mutate(!!x := 100 + !!x)) 工作相同;仅基于 dplyr programming recipes (1) 应该有效。

foo <- function(variables) {

  x <- rlang::sym(variables[[1]])

  x_ <- quo_name(x)

  print(x)
  #> b

  print(typeof(x))
  #> [1] "symbol"

  print(x_)
  #> [1] "b"

  print(typeof(x_))
  #> [1] "character"

  df <- data_frame(a = 1, b = 2)

  print(df %>% mutate(!!x_ := 100 + !!x))

  #> # A tibble: 1 x 2
  #>         a     b
  #>       <dbl> <dbl>
  #>   1     1   102  

  print(df %>% mutate(!!x := 100 + !!x))

  #> # A tibble: 1 x 2
  #>         a     b
  #>       <dbl> <dbl>
  #>   1     1   102  

}

移动评论回答。

根据 documentation 中提到的,您指的是:

The rules on the LHS are slightly different: the unquoted operand should evaluate to a string or a symbol.

在这里,它起作用了,因为 x_ 实际上是一个字符。