R 使用整洁的评估在我自己的函数中进行筛选
在编写自己的函数时,我很难使用整洁的评估。 我将使用ToothGrowth数据集来说明我的问题,在该数据集中,我们希望筛选VC作为补充R 使用整洁的评估在我自己的函数中进行筛选,r,dplyr,filtering,tidyeval,R,Dplyr,Filtering,Tidyeval,在编写自己的函数时,我很难使用整洁的评估。 我将使用ToothGrowth数据集来说明我的问题,在该数据集中,我们希望筛选VC作为补充 install.packages("tidyverse") library(tidyverse) data(ToothGrowth) test <- function(df, condition) { df %>% filter(supp %in% condition) } test(ToothGrowth, "
install.packages("tidyverse")
library(tidyverse)
data(ToothGrowth)
test <- function(df, condition) {
df %>% filter(supp %in% condition)
}
test(ToothGrowth, "VC")
到目前为止,我已经试过了quo()
,enquo()
,sym()
,ensym()
,但都没有成功
有没有办法为我的函数提供一个不带引号的参数,并在函数中引用它,以便dplyr::filter()
可以使用它?非常感谢您的帮助。您可以使用
deparse
+substitute
将不带引号的参数更改为带引号的参数
library(dplyr)
test <- function(df, condition) {
val <- deparse(substitute(condition))
df %>% filter(supp %in% val)
}
test(ToothGrowth, VC)
库(dplyr)
testVC
是一个值,而不是一个变量。如果你真的想做的话,你可以做你想要的UI,但是这会让你的功能在感觉上非常独特。如果您想遵循约定,请为数据框列保留数据屏蔽。嗨,Ronak,我能够在我的脚本中实现您的解决方案。它就像一个符咒。
library(dplyr)
test <- function(df, condition) {
val <- deparse(substitute(condition))
df %>% filter(supp %in% val)
}
test(ToothGrowth, VC)