Dear David--
I think I am straight on the dopiness of my original question.

In trying to make sure I understand your example, I ran this function
below. It seems to assign the value of V from the calling environment to X
as a default, ignored if X is given a value positionally, used if not. It
does not assign a name to X so far as I can tell, unless V has a name, in
which case it assigns to X the name of  V (not the name "V") to X, even if
X is given a different numerical value positionally. Does it seem to you
that I am understanding or misunderstanding your point about the = in a
function definition assigning names?

V<-2

Names(V)<- “Bob”

  foo <- function(X = V) {

    cat("name of x:", names(X), "\n")

    print("V:")

    print(V)

    print("X:")

    print(X)

  }

 foo(4)

 foo()


On Sun, Dec 29, 2013 at 8:05 PM, David Winsemius <dwinsem...@comcast.net>wrote:

>
> 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
>
>


-- 
J. Andrew Hoerner
Director, Sustainable Economics Program
Redefining Progress
(510) 507-4820

        [[alternative HTML version deleted]]

______________________________________________
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