Tidy-evaluation with base R functions?

Is there a straightforward way to wrap a base R function with non-standard evaluation so as to cause it to support tidy evaluation? Perhaps even some sort of meta-wrapper that outputs a base R-equivilent function with tidy-eval capability? Or would that need some deeper intervention into the base R code of the function?

You can wrap any function with a function that turns it arguments into quosures. From there you can do most any kind of non-standard evaluation before calling the warped function.

What is your goal in doing this, i.e. what sort of non-standard evaluation do you want to do before calling a base, or other, function?

Here are a couple of simple, not very useful, examples :grin:


mysum <- function(a, b) {
    aq <- rlang::enquo(a)
    bq <- rlang::enquo(b)
    ra <- utils::as.roman(rlang::expr_text(aq[[2]]))
    rb <- utils::as.roman(rlang::expr_text(bq[[2]]))
    sum(ra, rb)
}

mysum(X, V)
#> [1] 15
mysum(XI, VII)
#> [1] 18

mysumrn <- function(a, b) {
    aq <- rlang::enquo(a)
    bq <- rlang::enquo(b)
    ra <- utils::as.roman(rlang::expr_text(aq[[2]]))
    rb <- utils::as.roman(rlang::expr_text(bq[[2]]))
    utils::as.roman(sum(ra, rb))
}

mysumrn(X, V)
#> [1] XV
mysumrn(XI, VII)
#> [1] XVIII
3 Likes

I tend to think of the quosure mechanism as a way of passing arguments to subsidiary functions without premature evaluation. I guess my goal is to try to limit the number of different evaluation mechanisms that I have to learn and keep track of. R has one kind of standard evaluation, but some large and uncertain number of non-standard evaluation methods. I don't think that it makes sense to think of trying to replace them all, e.g. it would not make sense to use quosure to replace the library accepting a quoted or unquoted argument, or $ quoting is RHS argument. But in other cases I am trying to understand whether quosures can become the "standard" NSE mechanism.