[R] environment and scoping

hadley wickham h.wickham at gmail.com
Sat Oct 4 19:47:41 CEST 2008


What's wrong with explicitly passing the variables as arguments to the function?

aux.fun.one <- function(dat, x){
  median(dat) - x
}

Hadley

On Sat, Oct 4, 2008 at 11:50 AM, René Holst <rho at aqua.dtu.dk> wrote:
> I haven't quite figured out how I can change the environment of a function.
> I have a main function and want to use different auxillary functions, which I supply as parameter (or names). What I want to do is something like this:
>
> main.fun=function(aux.fun,dat){
>  x <- 1
>  fun.dat()
> }
>
> aux.fun.one=function(){
>  mean(dat)+x
> }
>
> aux.fun.one=function(){
>  median(dat)-x
> }
>
> I don't want to transfer the dat variable as a parameter as it can get pretty large. The enclosing environment of both auxillary functions is the Global environ, so they cannot see neither dat nor x as they exist inside the scope of the main function. So how do I do this?
>
> Thanks,
>
> René
>
>        [[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.
>
>



-- 
http://had.co.nz/



More information about the R-help mailing list