Andrew Hoerner
2013-Dec-16 17:31 UTC
[R] Assigning default function arguments to themselves: Why?
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. I have noticed that many functions contain arguments with defaults of the form X=X. Call this reflexive assignment. 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 of foo, will it not immediately pop up to the parent frame if it is not found in foo? Are reflexive assignments just to keep X from being positionaly assigned accidentally, or are They doing something deeper? A question which is (I think) related: Is it good or bad practice, when defining a function inside another function, to use the name of a variable from the calling function as the argument of the called function? [[alternative HTML version deleted]]
William Dunlap
2013-Dec-16 17:52 UTC
[R] Assigning default function arguments to themselves: Why?
> I have noticed that many functions contain arguments with defaults of > the form X=X.Can you show us one (one that 'works')? Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Andrew Hoerner > Sent: Monday, December 16, 2013 9:31 AM > To: r-help at r-project.org > Subject: [R] Assigning default function arguments to themselves: Why? > > 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. > > I have noticed that many functions contain arguments with defaults of > the form X=X. Call this reflexive assignment. 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 of foo, will it not immediately pop up to the > parent frame if it is not found in foo? Are reflexive assignments just > to keep X from being positionaly assigned accidentally, or are They > doing something deeper? > > A question which is (I think) related: Is it good or bad practice, > when defining a function inside another function, to use the name of a > variable from the calling function as the argument of the called > function? > > [[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.