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

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

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

frederik-2
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

Jeff Newmiller
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, [hidden email] wrote:

>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
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

frederik-2
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, [hidden email] wrote:
> >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
> >
> >______________________________________________
> >[hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

David Winsemius

> On Dec 11, 2016, at 5:35 PM, [hidden email] wrote:
>
> 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...

From a typical US 10th grade English assignment:

Cassius:
"The fault, dear Brutus, is not in our stars,
But in ourselves, that we are underlings."

--
David.

>
> 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, [hidden email] wrote:
>>> 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
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>
> ______________________________________________
> [hidden email] 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.

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

frederik-2
Thank you, that made me laugh :)

On Sun, Dec 11, 2016 at 08:53:42PM -0800, David Winsemius wrote:

>
> > On Dec 11, 2016, at 5:35 PM, [hidden email] wrote:
> >
> > 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...
>
> From a typical US 10th grade English assignment:
>
> Cassius:
> "The fault, dear Brutus, is not in our stars,
> But in ourselves, that we are underlings."
>
> --
> David.
> >
> > 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, [hidden email] wrote:
> >>> 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
> >>>
> >>> ______________________________________________
> >>> [hidden email] 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.
> >>
> >
> > ______________________________________________
> > [hidden email] 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.
>
> David Winsemius
> Alameda, CA, USA
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

Fox, John
In reply to this post by frederik-2
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:[hidden email]] On Behalf Of
> [hidden email]
> Sent: December 11, 2016 8:35 PM
> To: [hidden email]
> 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, [hidden email] 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
> > >
> > >______________________________________________
> > >[hidden email] 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.
> >
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

David Winsemius

> On Dec 12, 2016, at 2:07 PM, Fox, John <[hidden email]> 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

Exactly the same answer as the crossposting elicited:

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

--
David

>
> Best,
> John
>
> -----------------------------
> John Fox, Professor
> McMaster University
> Hamilton, Ontario
> Canada L8S 4M4
> Web: socserv.mcmaster.ca/jfox
>
>
>
>
>> -----Original Message-----
>> From: R-help [mailto:[hidden email]] On Behalf Of
>> [hidden email]
>> Sent: December 11, 2016 8:35 PM
>> To: [hidden email]
>> 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, [hidden email] 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
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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.
>>>
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

Bert Gunter-2
In reply to this post by Fox, John
*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 <[hidden email]> 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:[hidden email]] On Behalf Of
>> [hidden email]
>> Sent: December 11, 2016 8:35 PM
>> To: [hidden email]
>> 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, [hidden email] 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
>> > >
>> > >______________________________________________
>> > >[hidden email] 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.
>> >
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

Fox, John
Dear Bert,

It's nitpicking, I guess, but the call to message() is in the original posting. Your solution produces

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

> f <- function(x){
+     message(desub(x))
+ }

> f(log)
x

Best,
 John

> -----Original Message-----
> From: Bert Gunter [mailto:[hidden email]]
> Sent: Monday, December 12, 2016 6:41 PM
> To: Fox, John <[hidden email]>
> Cc: [hidden email]; [hidden email]
> Subject: Re: [R] how do I define a function which is equivalent to
> `deparse(substitute(x))`?
>
> *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 <[hidden email]> 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:[hidden email]] On Behalf Of
> >> [hidden email]
> >> Sent: December 11, 2016 8:35 PM
> >> To: [hidden email]
> >> 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, [hidden email] 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
> >> > >
> >> > >______________________________________________
> >> > >[hidden email] 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.
> >> >
> >>
> >> ______________________________________________
> >> [hidden email] 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.
> >
> > ______________________________________________
> > [hidden email] 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.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

Bert Gunter-2
John. et. al:

I assumed the message call was there to convert a quoted string to an
unquoted name and simply did this with as.name() in desub(). The point
of using sys.call() is that you can go up the call stack as far as you
need, so if you want to leave in the message() call, just go one
farther up the call stack:

> desub <- function(x) (all.vars(sys.call(-2))) ## note the -2 now

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


-- 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 3:58 PM, Fox, John <[hidden email]> wrote:

> Dear Bert,
>
> It's nitpicking, I guess, but the call to message() is in the original posting. Your solution produces
>
>> desub <- function(x) as.name(all.vars(sys.call(-1)))
>
>> f <- function(x){
> +     message(desub(x))
> + }
>
>> f(log)
> x
>
> Best,
>  John
>
>> -----Original Message-----
>> From: Bert Gunter [mailto:[hidden email]]
>> Sent: Monday, December 12, 2016 6:41 PM
>> To: Fox, John <[hidden email]>
>> Cc: [hidden email]; [hidden email]
>> Subject: Re: [R] how do I define a function which is equivalent to
>> `deparse(substitute(x))`?
>>
>> *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 <[hidden email]> 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:[hidden email]] On Behalf Of
>> >> [hidden email]
>> >> Sent: December 11, 2016 8:35 PM
>> >> To: [hidden email]
>> >> 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, [hidden email] 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
>> >> > >
>> >> > >______________________________________________
>> >> > >[hidden email] 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.
>> >> >
>> >>
>> >> ______________________________________________
>> >> [hidden email] 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.
>> >
>> > ______________________________________________
>> > [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

frederik-2
In reply to this post by David Winsemius
Thank you, John and David.

Yes someone already came up with that one on Stack Overflow.

Although I don't quite understand how it works - it would be nice to
see a step-by-step explanation of what is getting substituted and
evaluated in which environment. I guess 'eval.parent' must do its own
substituting, just so that it can see the variables you pass to it,
before it actually evaluates its argument. But 'eval' is .Internal...
Maybe if this is less of a mystery to someone else ...

Frederick

On Mon, Dec 12, 2016 at 02:54:14PM -0800, David Winsemius wrote:
> >...
> Exactly the same answer as the crossposting elicited:
>
> http://stackoverflow.com/questions/41083293/in-r-how-do-i-define-a-function-which-is-equivalent-to-deparsesubstitutex
>
> --
> David

On Mon, Dec 12, 2016 at 10:07:06PM +0000, Fox, John 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:[hidden email]] On Behalf Of
> > [hidden email]
> > Sent: December 11, 2016 8:35 PM
> > To: [hidden email]
> > 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, [hidden email] 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
> > > >
> > > >______________________________________________
> > > >[hidden email] 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.
> > >
> >
> > ______________________________________________
> > [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

Fox, John
In reply to this post by Bert Gunter-2
Dear Bert,

Your current version satisfies the original posting, but, though simpler than mine, is a bit fragile, in the following sense:

> desub <- function(x) (all.vars(sys.call(-2)))

> f <- function(x){
+     message(desub(x))
+ }

> f(log)
log

> g <- function(x){
+     desub(x)
+ }

> g(log)
character(0)


My version:

> desub <- function(y) {
+     deparse(eval(substitute(substitute(y)),
+                  env=parent.frame()))
+ }

> f(log)
log

> g(log)
[1] "log"

The deparse(substitute()) idiom returns the argument to f() or g() as a character string and so desub() should too, I guess.

Best,
 John

> -----Original Message-----
> From: Bert Gunter [mailto:[hidden email]]
> Sent: December 12, 2016 7:26 PM
> To: Fox, John <[hidden email]>
> Cc: [hidden email]; [hidden email]
> Subject: Re: [R] how do I define a function which is equivalent to
> `deparse(substitute(x))`?
>
> John. et. al:
>
> I assumed the message call was there to convert a quoted string to an
> unquoted name and simply did this with as.name() in desub(). The point of
> using sys.call() is that you can go up the call stack as far as you need, so if you
> want to leave in the message() call, just go one farther up the call stack:
>
> > desub <- function(x) (all.vars(sys.call(-2))) ## note the -2 now
>
> > g <- function(y)message((desub(y)))
> > g(log)
> log
>
>
> -- 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 3:58 PM, Fox, John <[hidden email]> wrote:
> > Dear Bert,
> >
> > It's nitpicking, I guess, but the call to message() is in the original
> > posting. Your solution produces
> >
> >> desub <- function(x) as.name(all.vars(sys.call(-1)))
> >
> >> f <- function(x){
> > +     message(desub(x))
> > + }
> >
> >> f(log)
> > x
> >
> > Best,
> >  John
> >
> >> -----Original Message-----
> >> From: Bert Gunter [mailto:[hidden email]]
> >> Sent: Monday, December 12, 2016 6:41 PM
> >> To: Fox, John <[hidden email]>
> >> Cc: [hidden email]; [hidden email]
> >> Subject: Re: [R] how do I define a function which is equivalent to
> >> `deparse(substitute(x))`?
> >>
> >> *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 <[hidden email]> 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:[hidden email]] On Behalf Of
> >> >> [hidden email]
> >> >> Sent: December 11, 2016 8:35 PM
> >> >> To: [hidden email]
> >> >> 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, [hidden email] wrote:
> >> >> > >Dear R-Help,
> >> >> > >
> >> >> > >I asked this question on StackOverflow,
> >> >> > >
> >> >> > >http://stackoverflow.com/questions/41083293/in-r-how-do-i-defin
> >> >> > >e-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
> >> >> > >
> >> >> > >______________________________________________
> >> >> > >[hidden email] 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.
> >> >> >
> >> >>
> >> >> ______________________________________________
> >> >> [hidden email] 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.
> >> >
> >> > ______________________________________________
> >> > [hidden email] 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.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

Bert Gunter-2
John:

If you insist:

> desub <- function(x)
      deparse(sys.call(-length(sys.parents())+1)[[2]])

>
> f <- function(y)desub(y)
> g <- function(y)message(desub(y))
>
> f(log)
[1] "log"

> g(log)
log


However, I would agree that searching backward through the call stack
can be tricky. For example, suppose we have in addition to the above,

h <- function(foo) g(foo)

Then using desub() as I defined it above, one gets as desired:

> h(log)
log

However, using your gsub(),

> desub <- function(y) {
       deparse(eval(substitute(substitute(y)),
         env=parent.frame()))
 }

One then gets:

> h(log)
foo  ## whoops!

I suspect one can find a way to break my approach that doesn't break
yours -- and if/when you find it, please post it. But on general
principles I prefer accessing the call stack directly rather than
indirectly through nested substitute calls. But beauty is in the eye
of the beholder...


Cheers,
Bert





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 5:45 PM, Fox, John <[hidden email]> wrote:

> Dear Bert,
>
> Your current version satisfies the original posting, but, though simpler than mine, is a bit fragile, in the following sense:
>
>> desub <- function(x) (all.vars(sys.call(-2)))
>
>> f <- function(x){
> +     message(desub(x))
> + }
>
>> f(log)
> log
>
>> g <- function(x){
> +     desub(x)
> + }
>
>> g(log)
> character(0)
>
>
> My version:
>
>> desub <- function(y) {
> +     deparse(eval(substitute(substitute(y)),
> +                  env=parent.frame()))
> + }
>
>> f(log)
> log
>
>> g(log)
> [1] "log"
>
> The deparse(substitute()) idiom returns the argument to f() or g() as a character string and so desub() should too, I guess.
>
> Best,
>  John
>
>> -----Original Message-----
>> From: Bert Gunter [mailto:[hidden email]]
>> Sent: December 12, 2016 7:26 PM
>> To: Fox, John <[hidden email]>
>> Cc: [hidden email]; [hidden email]
>> Subject: Re: [R] how do I define a function which is equivalent to
>> `deparse(substitute(x))`?
>>
>> John. et. al:
>>
>> I assumed the message call was there to convert a quoted string to an
>> unquoted name and simply did this with as.name() in desub(). The point of
>> using sys.call() is that you can go up the call stack as far as you need, so if you
>> want to leave in the message() call, just go one farther up the call stack:
>>
>> > desub <- function(x) (all.vars(sys.call(-2))) ## note the -2 now
>>
>> > g <- function(y)message((desub(y)))
>> > g(log)
>> log
>>
>>
>> -- 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 3:58 PM, Fox, John <[hidden email]> wrote:
>> > Dear Bert,
>> >
>> > It's nitpicking, I guess, but the call to message() is in the original
>> > posting. Your solution produces
>> >
>> >> desub <- function(x) as.name(all.vars(sys.call(-1)))
>> >
>> >> f <- function(x){
>> > +     message(desub(x))
>> > + }
>> >
>> >> f(log)
>> > x
>> >
>> > Best,
>> >  John
>> >
>> >> -----Original Message-----
>> >> From: Bert Gunter [mailto:[hidden email]]
>> >> Sent: Monday, December 12, 2016 6:41 PM
>> >> To: Fox, John <[hidden email]>
>> >> Cc: [hidden email]; [hidden email]
>> >> Subject: Re: [R] how do I define a function which is equivalent to
>> >> `deparse(substitute(x))`?
>> >>
>> >> *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 <[hidden email]> 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:[hidden email]] On Behalf Of
>> >> >> [hidden email]
>> >> >> Sent: December 11, 2016 8:35 PM
>> >> >> To: [hidden email]
>> >> >> 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, [hidden email] wrote:
>> >> >> > >Dear R-Help,
>> >> >> > >
>> >> >> > >I asked this question on StackOverflow,
>> >> >> > >
>> >> >> > >http://stackoverflow.com/questions/41083293/in-r-how-do-i-defin
>> >> >> > >e-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
>> >> >> > >
>> >> >> > >______________________________________________
>> >> >> > >[hidden email] 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.
>> >> >> >
>> >> >>
>> >> >> ______________________________________________
>> >> >> [hidden email] 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.
>> >> >
>> >> > ______________________________________________
>> >> > [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how do I define a function which is equivalent to `deparse(substitute(x))`?

Fox, John
Dear Bert,

I'm tempted to let this thread drop, since I like your last solution better than mine: it's clearer and more robust -- in fact, I don't see a way to break it -- and the approach you used to write it generalizes better to other problems. But if the object is to produce a function that behaves like deparse(substitute()), which is the subject of the original posting, then

> g <- function(x) deparse(substitute(x))
> h <- function(foo) g(foo)
> h(log)
[1] "foo"

Best,
 John

> -----Original Message-----
> From: Bert Gunter [mailto:[hidden email]]
> Sent: December 13, 2016 12:52 AM
> To: Fox, John <[hidden email]>
> Cc: [hidden email]; [hidden email]
> Subject: Re: [R] how do I define a function which is equivalent to
> `deparse(substitute(x))`?
>
> John:
>
> If you insist:
>
> > desub <- function(x)
>       deparse(sys.call(-length(sys.parents())+1)[[2]])
>
> >
> > f <- function(y)desub(y)
> > g <- function(y)message(desub(y))
> >
> > f(log)
> [1] "log"
>
> > g(log)
> log
>
>
> However, I would agree that searching backward through the call stack can be
> tricky. For example, suppose we have in addition to the above,
>
> h <- function(foo) g(foo)
>
> Then using desub() as I defined it above, one gets as desired:
>
> > h(log)
> log
>
> However, using your gsub(),
>
> > desub <- function(y) {
>        deparse(eval(substitute(substitute(y)),
>          env=parent.frame()))
>  }
>
> One then gets:
>
> > h(log)
> foo  ## whoops!
>
> I suspect one can find a way to break my approach that doesn't break yours --
> and if/when you find it, please post it. But on general principles I prefer
> accessing the call stack directly rather than indirectly through nested substitute
> calls. But beauty is in the eye of the beholder...
>
>
> Cheers,
> Bert
>
>
>
>
>
> 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 5:45 PM, Fox, John <[hidden email]> wrote:
> > Dear Bert,
> >
> > Your current version satisfies the original posting, but, though simpler than
> mine, is a bit fragile, in the following sense:
> >
> >> desub <- function(x) (all.vars(sys.call(-2)))
> >
> >> f <- function(x){
> > +     message(desub(x))
> > + }
> >
> >> f(log)
> > log
> >
> >> g <- function(x){
> > +     desub(x)
> > + }
> >
> >> g(log)
> > character(0)
> >
> >
> > My version:
> >
> >> desub <- function(y) {
> > +     deparse(eval(substitute(substitute(y)),
> > +                  env=parent.frame())) }
> >
> >> f(log)
> > log
> >
> >> g(log)
> > [1] "log"
> >
> > The deparse(substitute()) idiom returns the argument to f() or g() as a
> character string and so desub() should too, I guess.
> >
> > Best,
> >  John
> >
> >> -----Original Message-----
> >> From: Bert Gunter [mailto:[hidden email]]
> >> Sent: December 12, 2016 7:26 PM
> >> To: Fox, John <[hidden email]>
> >> Cc: [hidden email]; [hidden email]
> >> Subject: Re: [R] how do I define a function which is equivalent to
> >> `deparse(substitute(x))`?
> >>
> >> John. et. al:
> >>
> >> I assumed the message call was there to convert a quoted string to an
> >> unquoted name and simply did this with as.name() in desub(). The
> >> point of using sys.call() is that you can go up the call stack as far
> >> as you need, so if you want to leave in the message() call, just go one farther
> up the call stack:
> >>
> >> > desub <- function(x) (all.vars(sys.call(-2))) ## note the -2 now
> >>
> >> > g <- function(y)message((desub(y)))
> >> > g(log)
> >> log
> >>
> >>
> >> -- 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 3:58 PM, Fox, John <[hidden email]> wrote:
> >> > Dear Bert,
> >> >
> >> > It's nitpicking, I guess, but the call to message() is in the
> >> > original posting. Your solution produces
> >> >
> >> >> desub <- function(x) as.name(all.vars(sys.call(-1)))
> >> >
> >> >> f <- function(x){
> >> > +     message(desub(x))
> >> > + }
> >> >
> >> >> f(log)
> >> > x
> >> >
> >> > Best,
> >> >  John
> >> >
> >> >> -----Original Message-----
> >> >> From: Bert Gunter [mailto:[hidden email]]
> >> >> Sent: Monday, December 12, 2016 6:41 PM
> >> >> To: Fox, John <[hidden email]>
> >> >> Cc: [hidden email]; [hidden email]
> >> >> Subject: Re: [R] how do I define a function which is equivalent to
> >> >> `deparse(substitute(x))`?
> >> >>
> >> >> *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 <[hidden email]> 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:[hidden email]] On Behalf Of
> >> >> >> [hidden email]
> >> >> >> Sent: December 11, 2016 8:35 PM
> >> >> >> To: [hidden email]
> >> >> >> 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, [hidden email] wrote:
> >> >> >> > >Dear R-Help,
> >> >> >> > >
> >> >> >> > >I asked this question on StackOverflow,
> >> >> >> > >
> >> >> >> > >http://stackoverflow.com/questions/41083293/in-r-how-do-i-de
> >> >> >> > >fin e-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
> >> >> >> > >
> >> >> >> > >______________________________________________
> >> >> >> > >[hidden email] 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.
> >> >> >> >
> >> >> >>
> >> >> >> ______________________________________________
> >> >> >> [hidden email] 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.
> >> >> >
> >> >> > ______________________________________________
> >> >> > [hidden email] 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.
______________________________________________
[hidden email] 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.