Skip to content

avoid <<- in specific case

6 messages · Berry Boessenkool, Sarah Goslee, Frans Marcelissen +2 more

#
On 05/10/2012 8:19 AM, Berry Boessenkool wrote:
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
#
Hi Berry,

You might look at scatterplot3d in the scatterplot3d package for an
example of how a similar problem was handled, precisely as Duncan
suggests.

Sarah
On Fri, Oct 5, 2012 at 8:25 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:

  
    
#
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.
#
On 05/10/2012 9:38 AM, Frans Marcelissen wrote:
If functions didn't have access to objects outside the local frame, they 
couldn't do much of anything at all.  Unlike Pascal, functions in R are 
first class objects.  Even "<-" is an object (found in the base 
package).  So functions need to be able to look outside themselves to 
find the thinks like mean(), var(), "+", "<-", etc.

Duncan Murdoch
#
Inline.

On Fri, Oct 5, 2012 at 6:38 AM, Frans Marcelissen
<frans.marcelissen at digipsy.nl> wrote:
Oy!
What you find "logical" others (like me) would find objectionable. You
have just asked to break R's entire scoping procedure. Do you really
think that is reasonable?

It is the duty of a programmer to learn and use the paradigms of the
language in which he/she programs. These will, of course, be different
from language to language -- appropriately  so. To complain that one
language does not follow the procedures of another, especially without
understanding the "big picture" of each language's design (which few
of us do) just does not make sense to me.

All IMHO, of course.

Cheers,
Bert