[R] <<- how/when/why do you use it?

Szumiloski, John john_szumiloski at merck.com
Wed Apr 14 18:31:11 CEST 2010


I love Patrick Burns' comment on the <<- operator in R Inferno:

"If you think you need '<<-', think again.  If on reflection you still
think you need '<<-', think again." 

Is this in package::fortunes?

John 

John  Szumiloski,  Ph.D.

Senior Biometrician
Biometrics Research
WP53B-120
Merck Research Laboratories
P.O. Box 0004
West Point, PA 19486-0004

(215) 652-7346 (PH)
(215) 993-1835 (FAX)



-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Greg Snow
Sent: Wednesday, 14 April, 2010 11:43 AM
To: Tal Galili; r-help at r-project.org
Subject: Re: [R] <<- how/when/why do you use it?

The <<- assignment operator is very powerful, but can be dangerous as
well.  When tempted to use it, look for alternatives first, there may be
a better way.  But having said that, I am one of the more guilty people
for using it (quite a few of the functions in the TeachingDemos package
use <<-).

The main use that I see is when you are using a function written by
someone else that takes one of your functions as an argument and you
want to save information from your function that is not being passed
back through the calling function.  For example you may want to trace
the calls to your function that is being called by optim, just define
your function A which defines within it function B which is to be
optimized, A also contains an empty vector to store results in, then A
calls optim passing B to it, B uses <<- to update the vector in A every
time that it is called, now A has the results of optim and also a trace
of info on all the calls to B.

<<- can also be used for package local variables (less evil than
globals) where within a package you can call one function to set some
things up, then other functions in the package can refer to the variable
created to see the setup as well as modifying options local to the
package.

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Tal Galili
> Sent: Tuesday, April 13, 2010 8:03 AM
> To: r-help at r-project.org
> Subject: [R] <<- how/when/why do you use it?
> 
> Hi all,
> 
> Today I came across scoping in the R
> intro<http://cran.r-project.org/doc/manuals/R-intro.html#Scope> (after
> reading Robert Gentleman
> fortune<http://rfortunes.posterous.com/im-always-thrilled-when-people-
> discover-what>
> on
> lexical scooping) , and am very curious about the <<- assignment.
> 
> The manual showed one (very interesting) example for "<<-", which I
> feel I
> understood. What I am still missing is the context of when this can be
> useful.
> 
> So what I would love to read from you are examples (or links to
> examples) on
> when using "<<-" can be interesting/useful. What might be the dangers
> of
> using it (it looks easy to loose track of), and any tips you might
feel
> like
> sharing.
> 
> Thanks,
> Tal
> 
> 
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Galili at gmail.com |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew)
> |
> www.r-statistics.com (English)
>
-----------------------------------------------------------------------
> -----------------------
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

______________________________________________
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.
Notice:  This e-mail message, together with any attachme...{{dropped:10}}



More information about the R-help mailing list