[R] What purpose is served by reflexive function assignments?
William Dunlap
wdunlap at tibco.com
Mon Dec 30 02:47:24 CET 2013
On Dec 28, 2013, at 7: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)
> }
The above is not valid R syntax. Can you correct it to make a self-contained
runnable example and re-ask the question?
> 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)?
I will venture that no useful function contains a default value of X=X.
Are you confounding definitions of functions (where default values are specified) like
foo <- function(X, Y=log(X)) { Y }
and calls to functions (where actual values are specified) like
foo(X=10, Y=15)
or
Y <- 7
foo(Y=Y)
?
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 andrewH
> Sent: Sunday, December 29, 2013 3:57 PM
> To: r-help at r-project.org
> Subject: Re: [R] What purpose is served by reflexive function assignments?
>
> Dear David--
>
> Thanks so much for your helpful reply!
>
> David Winsemius wrote:
> >>The LHS X becomes a name, the RHS X will be looked up in the calling
> environment and fails if no value is positionally matched and then no X is
> found (at the time of the function definition.
>
> Does X really have to exist when the function is defined? I thought it was
> enough if it existed in the environment of the calling function, or
> somewhere up the environment chain of the calling function. If this is not
> true, then that means it matters a lot whether you write a function inside
> another function or just call it in that function. Suppose a function with
> a reflexive assignment X=X is defined in the global environment but called
> inside another function, and X has a different value in those two places.
> Will it look first in the global environment and only then in the calling
> environment? And is this different from the behavior without the reflexive
> assignment?
>
> I should not bother you with those questions. I should just run it both ways
> and see what happens.calling function and will it look first in the
>
> >>If you use`X <- value` in the argument list, then what is returned is only
> the value and the name `X` may be lost. Or in the case of data.frame morphed
> into a strange name:
>
> [example omitted]
> I am not sure that I am understanding you correctly here. Are you saying
> that assignment using the "=" retains the name (and other attributes? which
> ones?) of the RHS, while "<-" does not?
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/What-purpose-is-served-
> by-reflexive-function-assignments-tp4682794p4682819.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