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

Bert Gunter bgunter.4567 at gmail.com
Tue Dec 13 00:41:28 CET 2016


*If* I understand correctly -- and please let me know if I don't --
this seems somewhat more straightforward and less "hacky" :

> desub <- function(x) as.name(all.vars(sys.call(-1)))

Yielding in the OP's example:

> g <- function(y)desub(y)
> g(log)
log

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Dec 12, 2016 at 2:07 PM, Fox, John <jfox at mcmaster.ca> wrote:
> Dear Frederick,
>
> I found this a challenging puzzle, and it took me awhile to come up with an alternative, and I think slightly simpler, solution:
>
>> desub <- function(y) {
> +     deparse(eval(substitute(substitute(y)),
> +                  env=parent.frame()))
> + }
>
>> f <- function(x){
> +     message(desub(x))
> + }
>
>> f(log)
> log
>
> Best,
>  John
>
> -----------------------------
> John Fox, Professor
> McMaster University
> Hamilton, Ontario
> Canada L8S 4M4
> Web: socserv.mcmaster.ca/jfox
>
>
>
>
>> -----Original Message-----
>> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of
>> frederik at ofb.net
>> Sent: December 11, 2016 8:35 PM
>> To: r-help at r-project.org
>> Subject: Re: [R] how do I define a function which is equivalent to
>> `deparse(substitute(x))`?
>>
>> Dear R-Help,
>>
>> I was going to ask Jeff to read the entire works of William Shakespeare to learn
>> why his reply was not helpful to me...
>>
>> Then I realized that the answer, as always, lies within...
>>
>>     desub <- function(y) {
>>       e1=substitute(y, environment())
>>       e2=do.call(substitute,list(e1), env=parent.frame())
>>       deparse(e2)
>>     }
>>
>> Sorry to trouble the list; other solutions still welcome.
>>
>> Cheers,
>>
>> Frederick
>>
>> On Sun, Dec 11, 2016 at 12:46:23AM -0800, Jeff Newmiller wrote:
>> > No. Read Hadley Wickham's "Advanced R" to learn why not.
>> > --
>> > Sent from my phone. Please excuse my brevity.
>> >
>> > On December 10, 2016 10:24:49 PM PST, frederik at ofb.net wrote:
>> > >Dear R-Help,
>> > >
>> > >I asked this question on StackOverflow,
>> > >
>> > >http://stackoverflow.com/questions/41083293/in-r-how-do-i-define-a-fu
>> > >nction-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
>> > >
>> > >______________________________________________
>> > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > >https://stat.ethz.ch/mailman/listinfo/r-help
>> > >PLEASE do read the posting guide
>> > >http://www.R-project.org/posting-guide.html
>> > >and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list