Re: [R] Define "local" function

From: Peter Dalgaard <>
Date: Sat 16 Apr 2005 - 03:16:00 EST

Ales Ziberna <> 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

  1. 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 working.

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

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Sat Apr 16 03:34:39 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:12 EST