Frederic F
2011-Aug-12 14:45 UTC
[R] Can the environment of a function be specified when the function is defined?
Hello, Is there a syntax to set the environment of a function when this function is defined? The best I could come up with so far is using a wrapper function: foo_internals<- function(x) {"Code of the function"} foo<- function(x) { environment(foo_internals)<-as.environment(target_environment) foo_internals(x) } But I would like to know if there is a cleaner syntax. There are two reasons why I would like to have a function defined with an environment different then the local one: The first is to develop a function that will go in a package: there might be more things loaded in my R_GlobalEnv than what will be available to the function when its environment will be the namespace of the package, so I would like to restrict right away the environment of the function that I develop in my local enviroment to what will be their environment in 'real life'. The other reason would be to have a function written in a package act as if it was from another package by giving it the environment of this other package. Thanks for your suggestions and comments, Frederic -- View this message in context: http://r.789695.n4.nabble.com/Can-the-environment-of-a-function-be-specified-when-the-function-is-defined-tp3739482p3739482.html Sent from the R help mailing list archive at Nabble.com.
Uwe Ligges
2011-Aug-12 15:16 UTC
[R] Can the environment of a function be specified when the function is defined?
I doubt what you have in mind makes too much sense, but changing the environment of a function foo to and environment env can be done using environment(foo) <- env Uwe Ligges On 12.08.2011 16:45, Frederic F wrote:> Hello, > > Is there a syntax to set the environment of a function when this function is > defined? > > The best I could come up with so far is using a wrapper function: > > foo_internals<- function(x) {"Code of the function"} > > foo<- function(x) { > environment(foo_internals)<-as.environment(target_environment) > foo_internals(x) > } > > But I would like to know if there is a cleaner syntax. > > There are two reasons why I would like to have a function defined with an > environment different then the local one: > The first is to develop a function that will go in a package: there might be > more things loaded in my R_GlobalEnv than what will be available to the > function when its environment will be the namespace of the package, so I > would like to restrict right away the environment of the function that I > develop in my local enviroment to what will be their environment in 'real > life'. > The other reason would be to have a function written in a package act as if > it was from another package by giving it the environment of this other > package. > > Thanks for your suggestions and comments, > > Frederic > > -- > View this message in context: http://r.789695.n4.nabble.com/Can-the-environment-of-a-function-be-specified-when-the-function-is-defined-tp3739482p3739482.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Maybe Matching Threads
- A where() functions that does what exists() does but return the environment when object lives?
- access objects in my environment
- environment question
- A where() functions that does what exists() does but return the environment when object lives?
- specified network name is no longer available