[R] variable scope

Sam Steingold sds at gnu.org
Wed Aug 29 06:46:57 CEST 2012


> * Jeff Newmiller <wqarjzvy at qpa.qnivf.PN.hf> [2012-08-28 15:21:39 -0700]:
>
> Sam Steingold <sds at gnu.org> wrote:
>
>>> * R. Michael Weylandt <zvpunry.jrlynaqg at tznvy.pbz> [2012-08-28
>>13:45:35 -0500]:
>>>
>>>> always you shouldn't need manual garbage collection.
>>
>>my observation is that gc in R sucks.
>>(it cannot release small objects).
>>this is not specific to R; ocaml suffers too.
>
> Algorithms that work on small objects suck, to.  Vectorize, vectorize,
> vectorize. Then you won't worry about these limitations.

I did vectorize.
No loops.
However, gsub/strsplit/substring &c allocate a lot of small objects
which are never GCed.

>>> since a loop doesn't define its own scope like some languages (a
>>> practice that always seemed strange to me),
>>
>>every level of indentation has its own scope.
>>seems reasonable.
>
> reasonable... to you. To me, as well, but the scoping in R has certain
> advantages for ad-hoc analyses, so leave your grumpy preconceptions
> behind and learn about environments and their parent environments.

absolutely!

for (...) {
  x <- f()
  g(x)
}

when g fails, having access to x is a HUGE bonus.

>>> The other answer is to use functions / apply statements like the good
>>> lord and John Chambers intended :-)
>>
>>so explicit loops are "deprecated" in some sense?
>
> Notice the emoticon. c.f. comments above on vectorizing.

I should have written "morally deprecated" :-)

> Loops that should be vectorized are strongly discouraged.

should I also vectorize loops like

for (z in c("a","b","c")) {
  x <- read.table(z)
  ...
}

?

Again, thanks for your insight!

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://think-israel.org http://palestinefacts.org
http://www.memritv.org http://pmw.org.il http://americancensorship.org
When you talk to God, it's prayer; when He talks to you, it's schizophrenia.




More information about the R-help mailing list