Search code examples
rtidyverserlangtidyeval

tidyr quasiquotation with lexical scoping


I realize that tidy evaluation does not use lexical scoping, but I want the quasiquotation in rlang to look for symbols in the environment I decide.

Current behavior:

envir <- new.env(parent = globalenv())
eval(parse(text = "little_b <- 'b'"), envir = envir)
eval(rlang::expr(!!little_b), envir = envir)

## Error in (function (x)  : object 'little_b' not found

Instead, I want the last line to return "b". Bonus points if you find a version of eval() that does the job here AND works like evaluate::try_capture_stack().

FYI: I am trying to solve this issue.


Solution

  • We can use with to pass the expression within an environment

    with(envir, expr(!!little_b))
    #[1] "b"
    

    Or another option is local

    local(rlang::expr(!!little_b), envir = envir)
    #[1] "b"
    

    Or pass via quote in eval (as @lionel mentioned)

    eval(quote(expr = rlang::expr(!! little_b)), envir = envir)
    #[1] "b"