[R] Can the environment of a function be specified when the function is defined?

Frederic F fournier.frederic at gmail.com
Fri Aug 12 16:45:08 CEST 2011


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.



More information about the R-help mailing list