You posted this same question two weeks ago, received a reasonable question in response, and ignored it.

If you want help on the list please respond to questions. If the discussion goes offline, please post a summary when it is done.

Duncan Murdoch

On 13-12-28 10:27 PM, Andrew Hoerner 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. Call this reflexive assignment of arguments. How is foo(X=X)
different from foo(X)? Isn't the environment from which X is located the
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.

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?

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.

Sincerely, andrewH


______________________________________________
R-help@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.

Reply via email to