[R] Assign references

Patrick Burns
Sat, 08 Oct 2005 09:22:39 -0000

Seeliger.Curt at epamail.epa.gov wrote:

>Patrick,
>
>I'll have to check your S poetry, it's not clear why 'changing
things
>invisibly' is a bad thing in R, but is OK in C.  Perhaps the answer lies
>therein.
>
>

I think Tony's reply has the main part of the answer.  R is
designed as a functional language -- meaning, essentially,
that side effects don't happen except for assignment.  The
"<<-" operator (and 'assign') are compromises from that
ideal.

In C it is standard practice to pass by reference and that is
a great deal of its power.  In R it is standard practice not to
have to worry about objects being changed behind your
back -- that freedom is a part of the power of R.

On the surface this looks contradictory -- one of the two
approaches must be the better way.  But the two languages
have different uses.  C computes extremely efficiently in terms
of having operations that closely match the machine.  R computes
efficiently by having operations that closely match how people
think.

Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

>I'd considered returning a sequence, but it seems more straight forward
>to be able to pass the arguments by reference.  The difficulty in doing
>this in R points to a philosophical point which thus far has eluded me.
>Certainly more thinking is in order.
>
>Thanks for your help on this,
>cur
>
>--
>Curt Seeliger, Data Ranger
>CSC, EPA/WED contractor
>541/754-4638
>seeliger.curt at epa.gov
>
>Patrick Burns <pburns at pburns.seanet.com> wrote on 10/07/2005
01:23:07
>PM:
>
>
>
>>Because the function is using 'foo' and 'bar' as the
global
>>variables, not 'x' and 'y'.  What you might have missed
from
>>Thomas's statement (if I can take some liberties) is that you
>>almost surely don't want to do that -- it is bad style because
>>it changes things invisibly.  S Poetry has more on that.
>>
>>Better is:
>>
>>myFunk <- function(a, b) {
>>    x <- a + b
>>    y <- a * b
>>    list(x=x, y=y)
>>}
>>
>>xy <- myFunk(4, 5)
>>x <- xy$x
>>y <- xy$y
>>
>>
>>Patrick Burns
>>patrick at burns-stat.com
>>+44 (0)20 8525 0696
>>http://www.burns-stat.com
>>(home of S Poetry and "A Guide for the Unwilling S User")
>>
>>Seeliger.Curt at epamail.epa.gov wrote:
>>
>>
>>
>>>Folks,
>>>
>>>I've run into trouble while writing functions that I hope will
create
>>>and modify a dataframe or two.  To that end I've written a toy
>>>
>>>
>function
>
>
>>>that simply sets a couple of variables (well, tries but fails).
>>>Searching the archives, Thomas Lumley recently explained the
<<-
>>>operator, showing that it was necessary for x and y to exist prior
to
>>>the function call, but I haven't the faintest why this isn't
working:
>>>
>>>
>>>
>>>
>>>
>>>>myFunk<-function(a,b,foo,bar) {foo<<-a+b;
bar<<-a*b;}
>>>>x<-0; y<-0;
>>>>myFunk(4,5,x,y)
>>>>x<-0; y<-0;
>>>>myFunk(4,5,x,y)
>>>>x
>>>>
>>>>
>>>>
>>>>
>>>[1] 0
>>>
>>>
>>>
>>>
>>>>y
>>>>
>>>>
>>>>
>>>>
>>>[1] 0
>>>
>>>What (no doubt simple) reason is there for x and y not changing?
>>>
>>>Thank you,
>>>cur
>>>--
>>>Curt Seeliger, Data Ranger
>>>CSC, EPA/WED contractor
>>>541/754-4638
>>>seeliger.curt at epa.gov
>>>
>>>______________________________________________
>>>R-help at stat.math.ethz.ch mailing list
>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>PLEASE do read the posting guide!
>>>
>>>
>http://www.R-project.org/posting-guide.html
>
>
>>>
>>>
>>>
>>>
>>>
>
>
>
>
>
>