“tidyselect”包提供了一个选择辅助函数where。where用于通过自定义函数选择数据框列。它是“tidyselect”的内部函数。这意味着where不会被加载到您的命名空间中,您只能通过tidyselect:::where.
但是,我从 dplyr 插图中看到了以下示例:columnwise Operations。
starwars %>%
summarise(across(where(is.character), ~ length(unique(.x))))
#> # A tibble: 1 x 8
#> name hair_color skin_color eye_color sex gender homeworld species
#> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1 87 13 31 15 5 3 49 38
Run Code Online (Sandbox Code Playgroud)
在此示例中,where编写时没有前缀“tidyselect:::”,但显然,代码中没有错误,并且它产生了有意义的结果。这对我来说似乎很奇怪。我想知道为什么代码可以正常运行。
我猜这是由于“代码引用”,它是 tidyeval 方法的一部分。粗略地说,代码引用将代码挂起为表达式,并稍后在“内部环境”中计算表达式。这只是一个直观的猜测,我不知道如何测试。
我希望有人可以帮助我解决“哪里”的问题,或者留下一些关于代码如何为我工作的参考。