使用 vars() 引用代码的整洁评估问题

tidy evaluation question on quoting code using vars()

我正在阅读这份整洁的评估文档 https://tidyeval.tidyverse.org/sec-why-how.html 并且无法理解为什么这里的第一段代码不起作用而第二段代码起作用:

starwars %>% summarise_at(ends_with("color"), n_distinct)

starwars %>% summarise_at(vars(ends_with("color")), n_distinct)

第一段代码显示错误信息:'No tidyselect variables were registered'

然后我了解到,出于某种原因,它无法在 starwars 数据框中找到以颜色结尾的列。

vars() 做了什么才能找到 starwars 数据框中的列?这与它所处的环境有关吗?

需要

vars(...) 以便正确填写 ends_withvars 参数。自己填的话就不需要vars

starwars %>% summarise_at(ends_with("color", vars = names(.)), n_distinct)

给予:

# A tibble: 1 x 3
  hair_color skin_color eye_color
       <int>      <int>     <int>
1         13         31        15

vars() 正在阻止立即评估其参数。如果您在控制台中调用它,您将看到它 returns 计算值(即表达式)的蓝图而不是值本身:

> dplyr::vars(ends_with("color"))
<list_of<quosure>>

[[1]]
<quosure>
expr: ^ends_with("color")
env:  global

这些延迟的计算稍后会在变量可用的上下文中恢复。如果你 运行 他们马上,上下文还没有设置,你会得到一个 tidyselect 错误。