<<- how/when/why do you use it?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

<<- how/when/why do you use it?

Tal Galili
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: [hidden email] |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: <<- how/when/why do you use it?

Duncan Murdoch
On 13/04/2010 10:02 AM, Tal Galili wrote:

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

It's useful in a couple of contexts.

In a big function, sometimes you have repetitive operations on local
variables:  you can write local functions to do them, and modify the
outer local variables using <<-.  (See tools::Rd2HTML for lots of
examples of this.)

There's also the use of them as a way to get references to objects that
maintain their state, by returning a function from a function:  it can
refer to and modify local variables in the outer function.  This is used
in the tkcanvas demo, for example.

One thing to watch out for:  if you have code like this:

x <<- 1 # modifies a non-local ("outer") variable
y <- x  # assignment from outer x

x <- 2  # creates a local

x <<- 3 # modifies the outer one
z <- x  # assignment from local x

then y will get 1, but z will get 2, even though the first and last
pairs of lines look very similar.  It's generally confusing to mix <<-
assignments to outer variables with <- assignments to locals having the
same name.  Just use different names.

Duncan Murdoch

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: <<- how/when/why do you use it?

Greg Snow-2
In reply to this post by Tal Galili
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
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Tal Galili
> Sent: Tuesday, April 13, 2010 8:03 AM
> To: [hidden email]
> 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: [hidden email] |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew)
> |
> www.r-statistics.com (English)
> -----------------------------------------------------------------------
> -----------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: <<- how/when/why do you use it?

Szumiloski, John
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: [hidden email] [mailto:[hidden email]]
On Behalf Of Greg Snow
Sent: Wednesday, 14 April, 2010 11:43 AM
To: Tal Galili; [hidden email]
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
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Tal Galili
> Sent: Tuesday, April 13, 2010 8:03 AM
> To: [hidden email]
> 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: [hidden email] |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew)
> |
> www.r-statistics.com (English)
>
-----------------------------------------------------------------------

> -----------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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}}

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: <<- how/when/why do you use it?

jholtman
In reply to this post by Greg Snow-2
If you use '<<-' remember that you are going to get into trouble and it will
be a challange to debug your script.  Only use it if you have a real good
understanding of the scoping rules in R and have exhausted all the other
avenues.

I, like Greg, am guilty of using it because I still keep some of my old
habits (like COMMON in FORTRAN) and every so often do have the need for a
"global" variable I can change in a lower level routine without returning a
value (and am willing to suffer the consequences of "side effects").  And
make sure that you always use the <<- when referring to that object so that
you do not create a local copy in some subroutine and really get messed up.

So as the answer to "when", I typically says "never".

On Wed, Apr 14, 2010 at 11:42 AM, Greg Snow <[hidden email]> wrote:

> 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
> [hidden email]
> 801.408.8111
>
>
> > -----Original Message-----
> > From: [hidden email] [mailto:r-help-bounces@r-
> > project.org] On Behalf Of Tal Galili
> > Sent: Tuesday, April 13, 2010 8:03 AM
> > To: [hidden email]
> > 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: [hidden email] |  972-52-7275845
> > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew)
> > |
> > www.r-statistics.com (English)
> > -----------------------------------------------------------------------
> > -----------------------
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-<http://www.r-project.org/posting->
> > guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: <<- how/when/why do you use it?

Gabor Grothendieck
In reply to this post by Greg Snow-2
x <<- will usually wind up assigning into the parent or global
environment but since it depends on what is already there the
following are safer:

e <- environment()
parent.env(e)$x <- 1

globalenv()$x <- 2

Typically in cases like this the function that contains the assignment
can be regarded as a method of the object containing x so an OO
approach can be taken such as facilitated by the proto package.  Here
p is defined to be a proto object with method square.x and property x.

> library(proto)
> p <- proto(x = 2, square.x = function(.) .$x <- .$x^2)
> p$x
[1] 2
> p$square.x()
> p$x
[1] 4



On Wed, Apr 14, 2010 at 11:42 AM, Greg Snow <[hidden email]> wrote:

> 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
> [hidden email]
> 801.408.8111
>
>
>> -----Original Message-----
>> From: [hidden email] [mailto:r-help-bounces@r-
>> project.org] On Behalf Of Tal Galili
>> Sent: Tuesday, April 13, 2010 8:03 AM
>> To: [hidden email]
>> 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: [hidden email] |  972-52-7275845
>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew)
>> |
>> www.r-statistics.com (English)
>> -----------------------------------------------------------------------
>> -----------------------
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: <<- how/when/why do you use it?

Steve Lianoglou-6
In reply to this post by Szumiloski, John
> "If you think you need '<<-', think again.  If on reflection you still
> think you need '<<-', think again."
>
> Is this in package::fortunes?

+1 for adding that to fortunes ... I remember reading through The R
Inferno and getting a good laugh from several such one liners ...

-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: <<- how/when/why do you use it?

Greg Snow-2
In reply to this post by Gabor Grothendieck
Thanks Gabor, I had not thought of using environments like that.  It looks like it can be done even simpler:

fun3 <- function() {

        e <- environment()
        tmp <- matrix( nrow=0, ncol=2 )

        fun4 <- function(x) {
                e$tmp <- rbind(e$tmp, x)
                (x[1] - 3)^2 + (x[2]-5)^2
        }

        out <- optim( c(0,0), fun4 )
        list( out=out, vals=tmp )
}

tmp <- fun3()
plot( tmp$vals, type='l' )

Now I can start removing all the "<<-"s from my code.

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111


> -----Original Message-----
> From: Gabor Grothendieck [mailto:[hidden email]]
> Sent: Wednesday, April 14, 2010 11:36 AM
> To: Greg Snow
> Cc: Tal Galili; [hidden email]
> Subject: Re: [R] <<- how/when/why do you use it?
>
> x <<- will usually wind up assigning into the parent or global
> environment but since it depends on what is already there the
> following are safer:
>
> e <- environment()
> parent.env(e)$x <- 1
>
> globalenv()$x <- 2
>
> Typically in cases like this the function that contains the assignment
> can be regarded as a method of the object containing x so an OO
> approach can be taken such as facilitated by the proto package.  Here
> p is defined to be a proto object with method square.x and property x.
>
> > library(proto)
> > p <- proto(x = 2, square.x = function(.) .$x <- .$x^2)
> > p$x
> [1] 2
> > p$square.x()
> > p$x
> [1] 4
>
>
>
> On Wed, Apr 14, 2010 at 11:42 AM, Greg Snow <[hidden email]>
> wrote:
> > 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
> > [hidden email]
> > 801.408.8111
> >
> >
> >> -----Original Message-----
> >> From: [hidden email] [mailto:r-help-bounces@r-
> >> project.org] On Behalf Of Tal Galili
> >> Sent: Tuesday, April 13, 2010 8:03 AM
> >> To: [hidden email]
> >> 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: [hidden email] |  972-52-7275845
> >> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il
> (Hebrew)
> >> |
> >> www.r-statistics.com (English)
> >> --------------------------------------------------------------------
> ---
> >> -----------------------
> >>
> >>       [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> [hidden email] 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.
> >
> > ______________________________________________
> > [hidden email] 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.
> >

______________________________________________
[hidden email] 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.