[R] What purpose is served by reflexive function assignments?

Ista Zahn istazahn at gmail.com
Mon Dec 30 14:31:07 CET 2013


On Sun, Dec 29, 2013 at 11:37 PM, andrewH <ahoerner at rprogress.org> wrote:
> Dear Ista--
> Peter's post has already persuaded me that my original question was based on
> several misunderstandings and so difficult if not impossible to follow --
> though he did a remarkable job of figuring out where I was going astray and
> what examples might set me right.
>
> But I will post the results of two of my experiments that I still find
> puzzling.
>
> This generates a recursive default error in the cat function. I do not see
> why it does not print 5:
> X <- 2
> gg <-  function(X=X){cat("gg: ", X)}
> ss<- function(X){
> X <- 5
> gg()
> }
> ss()
>
> And this generates an " 'x' is missing " error in x-y. I expected it to
> return the number -1:
> x<-1
> y<-2
> foo<- function(x=x,y=y){x-y}
> foo()

OK, so don't do that. And google your errors before asking about theme
here; there are plenty of discussions already on the web.

Best,
Ista

>
> Thanks so much for your time and attention!
> andrewH
>
>
> Ista Zahn wrote
>> On Sat, Dec 28, 2013 at 10:27 PM, Andrew Hoerner <
>
>> ahoerner@
>
>> > wrote:
>>> Let us suppose that we have a function foo(X) which is called inside
>>> another function, bar(). Suppose, moreover, that the name "X" has been
>>> assigned a value when foo is called:
>>>
>>> X <- 2
>>> bar(X=X){
>>> foo(X)
>>> }
>>>
>>> I have noticed that many functions contain arguments with defaults of the
>>> form X=X.
>>
>> An example would be really helpful here.
>>
>> Call this reflexive assignment of arguments.
>>
>> Why call this anything special? All this does is set the default value
>> of the X argument. I'm not sure what makes this "reflexive", or why it
>> needs a special descriptive term.
>>
>> How is foo(X=X)
>>> different from foo(X)? Isn't the environment from which X is located the
>>
>> foo(X) is hardcoded, foo(X = X) just sets a default.
>>
>>> parent environment of foo() in either case? Or if it looks first in the
>>> environment inside of foo, will it not immediately pop up to the parent
>>> environment if it is not found in foo? Are reflexive assignments just to
>>> keep X from being positionally assigned accidentally, or are they doing
>>> something deeper? Moreover, this is the only place I have seen people
>>> consistently using an equals sign in place of the usual "<-", and I am
>>> confident that there is some subtle difference in how the two assignment
>>> operators work, perhaps beyond the ken of lesser mortals like myself,
>>> that
>>> explains why the "=" is preferred in this particular application.
>>
>> Again, some examples would really help here.
>>
>>>
>>> Actually, although I would like to hear the deep answer, which I am sure
>>> has something to do with scoping, as everything really confusing in R
>>> does,
>>> my real question is, is there some rule of thumb by which one could
>>> decide
>>> whether or not to do a reflexive assignment in a function definition and
>>> be
>>> right most of the time?
>>
>> I'm still not even sure what reflexive assignment means. Can you
>> clarify, preferably with some examples.
>>
>>>
>>> Lately I have gotten several "Error: Promise is already under evaluation"
>>> messages, and my current rule of thumb for dealing with this is to add
>>> reflexive assignment to the variable if it is missing and take it out if
>>> it
>>> is present. This seems to work, but it makes me feel unintelligent. Is
>>> there a better rule? I would be most grateful for anyone who could shed
>>> light on the subject.
>>
>> Perhaps someone can, but you will certainly make their job easier if
>> you provide a concrete example that produces this error.
>>
>> Best,
>> Ista
>>
>>>
>>> Sincerely, andrewH
>>>
>>> --
>>> J. Andrew Hoerner
>>> Director, Sustainable Economics Program
>>> Redefining Progress
>>> (510) 507-4820
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>>
>
>> R-help@
>
>>  mailing list
>>> 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@
>
>>  mailing list
>> 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.
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/What-purpose-is-served-by-reflexive-function-assignments-tp4682794p4682827.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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