Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making loop-friendly formula interface in tidyeval functions

Tags:

r

tidyeval

I am writing a simple function using tidyeval where I need to pass the arguments to the formula interface. Although I have managed to build a working version of the function, it doesn't seem to work with for loops.

function

foo <- function(data, x, y) {
  BayesFactor::ttestBF(
    paired = FALSE,
    data = data,
    formula = rlang::new_formula(rlang::enexpr(y), rlang::enexpr(x))
  )
}

foo(mtcars, am, wt)
#> Bayes factor analysis
#> --------------
#> [1] Alt., r=0.707 : 1383.367 ±0%
#> 
#> Against denominator:
#>   Null, mu1-mu2 = 0 
#> ---
#> Bayes factor type: BFindepSample, JZS

working with loops

I also tried here !!col.name[i]

df <- dplyr::select(mtcars, am, wt, mpg)
col.name <- colnames(df)

for (i in 2:length(col.name)) {
  foo(
    data = mtcars,
    x = am,
    y = col.name[i]
  )
}
#> Error in `[.data.frame`(data, , dv): undefined columns selected
like image 281
Indrajeet Patil Avatar asked Mar 06 '26 10:03

Indrajeet Patil


1 Answers

If you want to make a data-masking function work with loops over columns, you have to do metaprogramming at some point.

Really there are two options:

  • Either make your function take strings with standard evaluation. Then transform that string to a symbol internally. The metaprogramming is internal.

  • Or make it take expressions with non-standard evaluation. Then your callers have to transform strings to symbols and unquote them. The metaprogramIng is external.

There is no way around that, unless you're going to create a non standard interface that works inconsistently and unpredictably by trying to be too magical.

like image 62
Lionel Henry Avatar answered Mar 09 '26 01:03

Lionel Henry