[R] What purpose is served by reflexive function assignments?
Ista Zahn
istazahn at gmail.com
Sun Dec 29 06:35:09 CET 2013
On Sat, Dec 28, 2013 at 10:27 PM, Andrew Hoerner <ahoerner at rprogress.org> 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 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