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

frederik at ofb.net frederik at ofb.net
Sun Dec 11 07:24:49 CET 2016


Dear R-Help,

I asked this question on StackOverflow,

http://stackoverflow.com/questions/41083293/in-r-how-do-i-define-a-function-which-is-equivalent-to-deparsesubstitutex

but thought perhaps R-help would be more appropriate.

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

Is there a way I can define a function `desub` so that `desub(x)` has
the same value as `deparse(substitute(x))` in every context?

Thank you,

Frederick Eaton



More information about the R-help mailing list