【发布时间】:2021-03-12 16:33:33
【问题描述】:
我正在编写一个使用大量 dplyr 函数的包 - 为了通过 devtools::check() 中的所有测试,我必须经常使用 .data。一些函数嵌套在其他函数中。在下面的示例中,我需要在tidyselect 上下文和标准评估中(在创建id) 的部分)中使用variable。
df <- data.frame(
V1 = 1:8,
V2 = rep(1:4,2)
)
test <- function(df, variable){
x <- df %>%
mutate(y = {{variable}} + 1)
id <- rlang::as_name(rlang::enquo(variable))
id_eq <- outer(df[[id]], df[[id]], `==`)
list(x, id_eq)
}
如果没有在 CMD 检查中收到任何警告或注释,我不知道如何处理此问题。
如果我运行test(df = df, variable = V1),则该功能有效,但不适用于test(df = df, variable = .data$V1)
【问题讨论】:
-
嗨 Arthur,
.data和.env背后的一般直觉是 disambiguate whether the name refers to a column in a data frame or a variable in the environment。但是,您的函数在.env$V1下的行为方式有点不清楚。mutate()语句很明显,但是当用户提供.env$myvar时,outer()语句应该发生什么? -
@ArtemSokolov 这就是我的意思,我正在寻找一种方法,以便用户提供
variable以通过devtools::check()``´ at the same time that this variable could either work in theouter``` 部分(例如outer(df[['V1']], df[['V1']], `==`)或内部 @ 987654340@
标签: r dplyr rlang tidyselect