[Rd] prevent reassignment of function names

Thomas Lumley tlumley at u.washington.edu
Thu Apr 6 22:44:50 CEST 2006


On Thu, 6 Apr 2006, Tim Bergsma wrote:

> Hi.
>
> I'm trying to find a systematic way to prevent assignment to names of
> existing functions.  I've tried reassigning to the assignment operator,
> with mixed results.  The function definition for "<-" below works as
> hoped for the demonstrated assignments to a and c.  However, for the
> assignment based on the test function, it appears that the formal
> argument is not visible.  Any suggestions?
>

Well, my first suggestion would be not to do it. I don't see any real 
benefit, and messing around with something as basic as assignment is 
likely to break something.  If nothing else, there is certainly code out 
there where people have, say, "df" or "c" as a variable name, and it isn't 
doing any harm.

As a programming exercise it seems that the following works

"<-" <- function(x,value){
         if(tryCatch(is.function(x),error=function(e) FALSE))
                 stop("assignment to an existing function")
         else  eval.parent(substitute(.Primitive("<-")(x,value)))
}


 	-thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



More information about the R-devel mailing list