[R] What purpose is served by reflexive function assignments?
David Winsemius
dwinsemius at comcast.net
Mon Dec 30 05:05:52 CET 2013
On Dec 29, 2013, at 3:57 PM, andrewH wrote:
> 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?
No
> 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
Arrrgh. The is no "reflexive assignment". You are making up a concept.
> 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?
Using "=" assigns a name. Using "<-" retruns a value and whether the
value gets a name depends on the particular function.
> foo <- function(X <- V) { print(X)}
Error: unexpected assignment in "foo <- function(X <-"
> foo <- function(X = V) { print(X)}
> foo(4)
[1] 4
> foo <- function(X = V) { print(V)}
> foo(4)
Error in print(V) : object 'V' not found
--
David Winsemius, MD
Alameda, CA, USA
More information about the R-help
mailing list