[R] using ``<-'' in function argument
Ivan Calandra
ivan.calandra at uni-hamburg.de
Fri Dec 3 10:54:58 CET 2010
Arf, yes it makes sense now!
So the idea here is: never use "<-" in function argument...
Thanks for the explanation!
Regards,
Ivan
Le 12/3/2010 10:48, Michael Bedward a écrit :
> It's only obvious when someone points it out :)
>
> fubar is not created because, in the test x> 3 returned FALSE, which
> means the cat function doesn't get used, which means the y arg (fubar
> <- 6) is never required and therefore not evaluated.
>
> Evil isn't it ?
>
> Michael
>
> On 3 December 2010 20:18, Ivan Calandra<ivan.calandra at uni-hamburg.de> wrote:
>> See below
>>
>> Le 12/3/2010 06:54, Berwin A Turlach a écrit :
>>> On Thu, 2 Dec 2010 23:34:02 -0500
>>> David Winsemius<dwinsemius at comcast.net> wrote:
>>>
>>>> [...] Erik is telling you that your use of ncol<-4 got evaluated to
>>>> 4 and that the name of the resulting object was ignored, howevert the
>>>> value of the operation was passed on to matrix which used positional
>>>> matching since "=" was not used.
>>> Sounds like a fair summary of what Erik said, but it is subtly wrong.
>>> R has lazy evaluation of its arguments. There is nothing that forces
>>> the assignment to be evaluated and to pass the result into the
>>> function. On the contrary, the assignment takes place when the
>>> function evaluates the argument. For example:
>>>
>>> R> rm(list=ls(all=TRUE))
>>> R> ls()
>>> character(0)
>>> R> foo<- function(x, y){
>>> + if (x> 3) cat(y, "\n")
>>> + x}
>>> R> foo(4, bar<- 5)
>>> 5
>>> [1] 4
>>> R> ls()
>>> [1] "bar" "foo"
>>> R> bar
>>> [1] 5
>>> R> foo(2, fubar<- 6)
>>> [1] 2
>>> R> fubar
>>> Error: object 'fubar' not found
>>> R> ls()
>>> [1] "bar" "foo"
>> Could you explain what's happening here?! In the first case "bar" is
>> created, but in the second "fubar" is not... Why is that? Am I missing
>> something obvious?
>>>> Usually the problem facing newbies is that they want to save
>>>> keystrokes and so use "=" for assignment (also a potential pitfall
>>>> although not as likely to mess you up as the choice to use the
>>>> two-keystroke path for argument assignment).
>>> On the contrary, the opposite is also very likely. One of my favourite
>>> idioms is:
>>>
>>> plot(fm<- lm(y~x, data=some.data))
>>>
>>> to (1) fit a model, (2) assign the fitted model to an object and (3)
>>> look immediately at diagnostic plots.
>>>
>>> Students came to me and said that the code in the lab sheet didn't
>>> work and they were getting strange error messages "about objects not
>>> being found". They reassured me that they had typed in exactly what
>>> was on the lab sheet. Of course, once I got to their computer and
>>> looked at their screen, it was clear that they had typed:
>>>
>>> plot(fm = lm(y~x, data=some.data))
>> It's not much more complicated to type it in two lines, but it's much
>> clearer and safer!
>>> Cheers,
>>>
>>> Berwin
>>>
>>> ========================== Full address ============================
>>> Berwin A Turlach Tel.: +61 (8) 6488 3338 (secr)
>>> School of Maths and Stats (M019) +61 (8) 6488 3383 (self)
>>> The University of Western Australia FAX : +61 (8) 6488 1028
>>> 35 Stirling Highway
>>> Crawley WA 6009 e-mail: berwin at maths.uwa.edu.au
>>> Australia http://www.maths.uwa.edu.au/~berwin
>>>
>>> ______________________________________________
>>> 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.
>>>
>> --
>> Ivan CALANDRA
>> PhD Student
>> University of Hamburg
>> Biozentrum Grindel und Zoologisches Museum
>> Abt. Säugetiere
>> Martin-Luther-King-Platz 3
>> D-20146 Hamburg, GERMANY
>> +49(0)40 42838 6231
>> ivan.calandra at uni-hamburg.de
>>
>> **********
>> http://www.for771.uni-bonn.de
>> http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
>>
>> ______________________________________________
>> 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.
>>
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
More information about the R-help
mailing list