[Rd] Behaviour of environment(foo)<- (PR#1509)

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
03 May 2002 12:26:58 +0200


J.C.Rougier@durham.ac.uk writes:

> Hi Everyone, 
> 
> I've noticed that setting the environment of a function from a package
> actually creates a copy of that function in .GlobalEnv and sets the
> environment on this copy:
> 
> > find(.profile)
> [1] "package:newblips"
> > environment(.profile) <- bb1$getEnv() # this is an environment
> > find(.profile)
> [1] ".GlobalEnv"       "package:newblips"
> 
> I don't have a problem with this behaviour (now I understand it!) but
> I think that it would help to document it in the help file.  May I
> suggest that instead of
> 
>      The assignment form sets the environment of the function or
>      formula `fun' to the `value' given.
> 
> we have
> 
>      The assignment form sets the environment of the function or
>      formula \code{fun} to \code{value} if \code{fun} is in the
>      current environment, otherwise it makes a copy of \code{fun} in
>      the current environment and sets the environment of the copy.
> 
> I think that describes the current procedure: please correct me if I
> am wrong!

But this is generic behaviour for *all* attribute assignment. It makes
no sense to describe a specific instance of it. Consider

data(airquality)
f<-function() { x <- class(airquality)<-NULL; ls() }
f()
class(airquality)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._