samedi 10 décembre 2016

In R, how do I define a function which is equivalent to `deparse(substitute(x))`?

I want to write a function in R which grabs the name of a variable from the context of its caller's caller. I think the problem I have is best understood by asking how to compose deparse and substitute. You can see that a naive composition does not work:

# a compose operator
>  `%c%` = function(x,y)function(...)x(y(...))

# a naive attempt to combine deparse and substitute
> desub = deparse %c% substitute
> f=function(foo) { message(desub(foo)) }
> f(log)
foo

# this is how it is supposed to work
> g=function(foo) { message(deparse(substitute(foo))) }
> g(log)
log

I also tried a couple of variations involving eval.parent but with no luck. Any help is appreciated.





Aucun commentaire:

Enregistrer un commentaire