Re: [R] "local({})" documentation?

From: Martin Morgan <mtmorgan_at_fhcrc.org>
Date: Fri, 08 Apr 2011 12:40:46 -0700

On 04/08/2011 11:11 AM, Doug Elias wrote:
> Greetings, all ...
>
> I'm trying to find documentation on the use of the "local()" function, and
> am drawing a complete blank; would someone please send me a pointer to where
> I can learn what it is intended for, and how to correctly use it?

Hi Doug --

?local

It can be useful as a function-ette giving local scope w/out cluttering the name space or creating variables that persist after they are needed, e.g.,

bar <- local({

     txt = readLines(<some/big/file>)
     ## do some things to reduce big txt, e.g., sample lines
     txt[sample(length(txt), 100)]

})

'bar' is now 100 lines from <some/big/file>; txt no longer exists.

It can also provide a closure, as in the 'snow' package top-level source

getMPIcluster <- NULL
setMPIcluster <- NULL
local({

     cl <- NULL
     getMPIcluster <<- function() cl
     setMPIcluster <<- function(new) cl <<- new
})

now both getMPIcluster and setMPIcluster have access to the same variable 'cl'; 'cl' continues to exist after the end of local() because there are still references to it (through the environments associated with get/set MPIcluster). This is an interesting piece of code for understanding R's scoping, environments, and garbage collection.

Martin

>
> Many thanks!
>
> Doug
>
> [[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.

-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793

______________________________________________
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.
Received on Fri 08 Apr 2011 - 19:46:03 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 08 Apr 2011 - 20:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive