[R] Define "local" function

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Apr 15 19:16:00 CEST 2005

Ales Ziberna <ales.ziberna at guest.arnes.si> writes:

> I am also very interested how this could be done, possibly in such a
> way that this would be incorporated in the function itself and there
> wouldn't be a need to write "environment(f) <- NULL" before calling a
> function, as is proposed in the reply below and in a thread a few days
> ago!

Notice BTW, that environment(f) <- NULL may have unexpected
consequences. What it really means is that the lexical scope of f
becomes the base package. This interpretation of NULL may change in
the future, since it is somewhat illogical and it has a couple of
undesirable consequences that there's no way to specify a truly empty
environment. So

a) if you're calling a function outside of the base package, you get
the effect of

> f <- function(){mean(rnorm(10))}
> environment(f)<-NULL
> f()
Error in mean(rnorm(10)) : couldn't find function "rnorm"

b) even if it does work now, it may be broken by a future change to R.
Notice that *all* functions contain unbound variables in the form of
functions so if we get an empty NULL environment, even "<-" may stop

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907

More information about the R-help mailing list