将 enquo 表达式传递给子函数

Iri*_*ren 0 r tidyeval quasiquotes

这个问题与将变量传递给使用 `enquo()` 的函数有关。

我有一个更高的函数,其参数为 tibble ( dat) 和 dat ( ) 中感兴趣的列variables_of_interest_in_dat。在该函数中,有一个对我想要传递给的另一个函数的调用variables_of_interest_in_dat

higher_function <- function(dat, variables_of_interest_in_dat){
    variables_of_interest_in_dat <- enquos(variables_of_interest_in_dat)

    lower_function(dat, ???variables_of_interest_in_dat???)
    }
    
lower_function <- function(dat, variables_of_interest_in_dat){
    variables_of_interest_in_dat <- enquos(variables_of_interest_in_dat)
       
    dat %>%
         select(!!!variables_of_interest_in_dat)
    }
Run Code Online (Sandbox Code Playgroud)

传递给 lower_function 的推荐方法是什么variables_of_interest_in_dat

我已经尝试过lower_function(dat, !!!variables_of_interest_in_dat),但是当我运行时higher_function(mtcars, cyl)返回“错误:无法!!!在顶层使用。”

在相关帖子中,higher_function 在将变量传递给 lower 函数之前并未对变量进行 enquo。

谢谢

Eli*_*lia 5

这是你想要的吗?

library(tidyverse)

LF <- function(df,var){
      newdf <- df %>% select({{var}})
      return(newdf)
    }

HF <- function(df,var){
  LF(df,{{var}})
}

LF(mtcars,disp)  
HF(mtcars,disp)
Run Code Online (Sandbox Code Playgroud)

(又名“curly curly”)运算{{}}符将引用方法替换为enquo()