I suspect that the answer is "No, you have to make a standard evaluation escape hatch", but maybe someone has some ideas.
Imagine writing a function that use non-standard evaluation to get the symbol/name of an object passed into it:
inner_func <- function(inner_arg) {
substitute(inner_arg)
}
inner_func(iris)
#> iris
This falls apart if you call inner_func()
inside a wrapping function, of course.
wrapper_func <- function(wrapper_arg) {
inner_func(wrapper_arg)
}
wrapper_func(iris)
#> wrapper_arg
Is there a way to get the original symbol out of inner_func()
even when it is passed inside another function, without changing the behaviour of inner_func()
? I know that this is possible:
inner_func <- function(inner_arg) {
evalq(as.symbol(inner_arg))
}
wrapper_func <- function(wrapper_func) {
# Get the symbol as a Character
obj_name <- deparse(substitute(wrapper_func))
# eval(as.symbol()) inside inner_func() to turn it back into a symbol
inner_func(obj_name)
}
wrapper_func(iris)
#> iris
But it involves doing some extra processing in inner_func()
that compromises its ability to be called as it was originally:
inner_func(iris)
#> Error in as.vector(x, mode = mode) :
'list' object cannot be coerced to type 'symbol'
If possible, I would prefer to do all of the extra processing in wrapper_func()
.