r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
498 stars 136 forks source link

Cannot get the argument name by `ensym()` after argument is evaludated #1629

Closed psychelzh closed 1 year ago

psychelzh commented 1 year ago

See the following example. {rlang} will treat its value only then:

f <- function(arg) { rlang::ensym(arg) }
g <- function(arg) { force(arg); rlang::ensym(arg) }
h <- function(arg) { force(arg); substitute(arg) }
j <- function(arg) { force(arg); rlang::enexpr(arg) }

x <- data.frame(a = 1)
f(x)
#> x
g(x)
#> Error in `rlang::ensym()`:
#> ! Can't convert to a symbol.
#> Backtrace:
#>     ▆
#>  1. ├─global g(x)
#>  2. │ └─rlang::ensym(arg)
#>  3. └─rlang::abort(message = message)
h(x)
#> x
j(x)
#>   a
#> 1 1

Created on 2023-05-19 with reprex v2.0.2

lionel- commented 1 year ago

Unfortunately this is a limitation of the way we interact with R to get argument information.

psychelzh commented 1 year ago

Thanks for responding. I am wondering if there is any documentation about this behavior, or any suggestions on workarounds, though substitue() will be okay as an alternative in such cases.

lionel- commented 1 year ago

I'll be tracking this in https://github.com/r-lib/rlang/issues/1340