Search code examples
rdplyrtidyevalquasiquotes

Passing variables to functions that use `enquo()`


I have a conceptual problem. I want to pass variables to a function that turns some of these variables into quosures via enquo (from the dplyr/rlang packages). However, I want to do so via other functions.

Consider the following, which has a lower-level function (the one that uses the enquo()) and a higher-level one that feeds into it. When it passes its variable q to lower_function (e.g., the user calls higher_func1(mtcars, cyl==6)), the value of new_q will be the expression 'q'.

How do I preserve the user inputs of a higher level function so they can be accepted by enquo()? I know there are work-arounds where I would change lower_function, but I'm not interested in those. I'm interested in a solution that doesn't change the function with enquo().

library(dplyr)
library(rlang)

higher_func1 <- function(df, q) {
  # Need to do something to `q` here
  lower_function(df, q, dplyr::filter)
}

lower_function <- function(df, qq, f) {
  new_q <- rlang::enquos(qq)
  f(df, !!! new_q)
}

Solution

  • The only way this is possible is by taking ... and forwarding them to the lower function. Named arguments have to be quoted and unquoted at each step.