Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用整洁的评估在我自己的函数中进行筛选_R_Dplyr_Filtering_Tidyeval - Fatal编程技术网

R 使用整洁的评估在我自己的函数中进行筛选

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, "

在编写自己的函数时,我很难使用整洁的评估。 我将使用ToothGrowth数据集来说明我的问题,在该数据集中,我们希望筛选VC作为补充

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)

test
VC
是一个值,而不是一个变量。如果你真的想做的话,你可以做你想要的UI,但是这会让你的功能在感觉上非常独特。如果您想遵循约定,请为数据框列保留数据屏蔽。

嗨,Ronak,我能够在我的脚本中实现您的解决方案。它就像一个符咒。
library(dplyr)

test <- function(df, condition) {
  val <- deparse(substitute(condition))
  df %>% filter(supp %in% val)
}

test(ToothGrowth, VC)