Skip to content
Prev 307080 / 398503 Next

avoid <<- in specific case

I agree with this. But there is (in my opinion) a much more dangerous point:
a<b
If  there is an object  called b in the function, it uses this b. But if you
forgot to inialise b in the function, it looks in the global environment. So
the following happens:
b<<-100
a<-b (if there is no b in the function, it uses b in the global environment.
So a=100
b<9 (writes to b in the local environment)
Now b in the global environment is 100, while b in the function is 9
It would be more logical if a<-b would give an error if b does not exist in
the function (just like pascal does).
-------------------
Frans

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Duncan Murdoch
Verzonden: vrijdag 5 oktober 2012 14:26
Aan: Berry Boessenkool
CC: R Help
Onderwerp: Re: [R] avoid <<- in specific case
On 05/10/2012 8:19 AM, Berry Boessenkool wrote:
uses barplot and works fine.
later find the actual location on the vertical axis, and not the number of
bars used by barplot.
assignment'.
with <<- but still have it available? I know it is generally not good
practice.

You can return the function as the value of your function.   A bonus:  
if it is created within the body of your function, it will have access to
all the local variables there.

You shouldn't write to the global environment, because globalenv belongs to
the user, not to you.  If the user wants your function in the global
environment s/he can just assign the value of your function to a variable
there.

Duncan Murdoch
from the rcmd check (make the function package-compatible)?
used to my function. (And I learned a lot writing it a couple of years ago).
border=par("fg"), ... )
diff(range(HBreaks)) # Assign a function to the global environment with
values calculated inside the main function.
border=border,...)
Use hpos:
______________________________________________
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.