lobstr icon indicating copy to clipboard operation
lobstr copied to clipboard

Request: make `ast()` not automatically unwrap quosures

Open wch opened this issue 3 years ago • 3 comments

I was using rlang::blast(), and trying to understand how it works.

lazyeval::ast_() shows the actual call:

blast <- function(expr, env = caller_env()) {
  eval_bare(enexpr(expr), env)
}
exp <- quo(a + b)

f <- function(x) {
  lazyeval::ast_(sys.call())
  invisible()
}
blast(f(!!exp))
#> ┗ ()
#>  ┗ `f
#>  ┗ ()
#>   ┗ `~
#>   ┗ ()
#>    ┗ `+
#>    ┗ `a
#>    ┗ `b 

However lobstr::ast(!!x) unwraps the quosures so you don't see the actual call:

g <- function(x) {
  lobstr::ast(!!sys.call())
  invisible()
}
blast(g(!!exp))
#> █─g 
#> └─█─`+` 
#>   ├─a 
#>   └─b 

wch avatar Nov 12 '20 17:11 wch

Actually, now that I think if it, I think that it's a little confusing that both lazyeval::ast_ and lobstr::ast do some sort of quosure unwrapping. The actual call has a quosure object inlined the AST, but both of those functions obscure that fact. It would be helpful if the ast function did not automatically do that, because it is misleading -- I want to see the actual AST to understand what's going on, not a modified version of it.

h <- function(x) {
  sys.call()
}
z <- blast(h(!!exp))
z[[2]]
#> <quosure>
#> expr: ^a + b
#> env:  global

wch avatar Nov 13 '20 01:11 wch

Is this a duplicate of #27?

hadley avatar Apr 06 '21 20:04 hadley

I think it's not quite the same -- what I'm asking for here is for quosures to not be specially printed (because currently, they're magically hidden), but #27 is asking for them to be specially printed. But I think the overall goal is the same, which is to see quosures in the AST and understand them.

wch avatar Apr 07 '21 17:04 wch