[R] Creating an environment for a function.
Thomas Lumley
tlumley at u.washington.edu
Thu Apr 13 22:01:58 CEST 2006
On Thu, 13 Apr 2006, Rolf Turner wrote:
> Brian Ripley wrote:
>
>> Can I also suggest local()? This does a similar thing in a perhaps
>> more natural way.
>
> Sorry, I'm not with you. I'm slow, and as I said, I don't
> really grok environments.
>
> Let's look at a toy example. Suppose I want to create a function foo:
>
> function(x){x^n}
>
> and assign n the value of 4, say, in the environment of foo.
>
> Duncan Murdoch's solution was to create a function make.foo:
>
> make.foo <- function() {
> n <- 4
> foo <- function(x){x^n}
> foo
> }
>
> and then execute
>
> foo <- make.foo()
>
> How would I go about accompishing the same (toy) task making use of the
> local() function?
>
foo <- local({
n <- 4
function(x) {x^n}
})
-thomas
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
More information about the R-help
mailing list