【发布时间】:2020-04-28 20:54:46
【问题描述】:
这是关于vignette("in-packages")中代码的问题
数据集如下。
(mini_iris <- iris %>%
as_tibble() %>%
.[c(1, 2, 51, 52, 101, 102), ])
#> # A tibble: 6 x 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3 1.4 0.2 setosa
#> 3 7 3.2 4.7 1.4 versicolor
#> 4 6.4 3.2 4.5 1.5 versicolor
#> 5 6.3 3.3 6 2.5 virginica
#> 6 5.8 2.7 5.1 1.9 virginica
如果列名在字符向量中(可能来自函数调用),您可以将其提供给 one_of()
nest_egg <- function(data, cols) {
nest(data, egg = one_of(cols))
}
nest_egg(mini_iris, c("Petal.Length", "Petal.Width", "Sepal.Length", "Sepal.Width"))
#> # A tibble: 3 x 2
#> Species egg
#> <fct> <list<df[,4]>>
#> 1 setosa [2 × 4]
#> 2 versicolor [2 × 4]
#> 3 virginica [2 × 4]
然后,小插图描述了这一点
这里
one_of()的使用很重要;如果不使用它,并且 data 包含名为 cols 的列,nest()将嵌套它而不是 cols 中命名的列。
我认为使用tidy评估可以解决。
library(rlang)
nest_egg2 <- function(data, cols) {
cols <- enexprs(cols)
nest(data, egg = !!!cols)
}
nest_egg2(mini_iris, c("Petal.Length", "Petal.Width", "Sepal.Length", "Sepal.Width"))
但它显示错误
Error: `!!!` can't be supplied with a name. Only the operand's names are retained.
在下一节中,Vignette 描述了这一点
要提供一个与您包装的 tidyr 函数类似的接口,您应该使用 {{ arg }} 传递参数。 {{ }} 是一个特殊的 tidy eval 运算符,它捕获用户提供的表达式并将其转发给另一个启用 tidy eval 的函数。
nest_egg <- function(df, cols) {
nest(df, egg = {{ cols }})
}
nest_egg(mini_iris, -Species)
但我想知道我的nest_egg2 有什么问题
【问题讨论】:
-
是的,我正在尝试解决
nest_egg2 -
如果你这样做
nest_egg2 <- function(data, cols) { cols <- enquo(cols); nest(data, egg = !! cols) }就可以了 -
明确地说,您的目标是拥有一个可以提供裸列名称的函数,就像使用
dplyr等函数一样?还是要将列名作为字符向量给出?
标签: r tidyverse tidyr tidyeval