[R] "locking" functions

Jason Turner jasont at indigoindustrial.co.nz
Mon Feb 19 11:55:59 CET 2001

> Hi .. another newbie question .. is it possible to "lock" a function
> (or indeed any object), making it read-only until "unlocked"?

I don't know.  But your example and further questions point to 
another answer.

> For example, should I inadvertently (or through sheer stupidity <g> ) 
> override the definition of t
> > t
> function (x) 
> UseMethod("t")
[much stuff deleted]

The books "Modern Applied Statistics With S-Plus", and
"S Programming", both by Venables and Ripley, give a very good
treatment of this issue.

The short (and probably slightly flawed, but good enough) answer is that 
if you can understand how a search path works in an operating system, 
you can understand this.  Stuff you create is first on the search path. 
Next come the libraries you load, in the reverse order - the last library 
you loaded is the first searched (*after* the stuff you wrote yourself).

The order of the search path is shown with the "search()" command.

For this reason, it's a pretty good idea to name your own functions
something distinctly yours:  my.t or ja.t are not bad.

> The t example above is obviously contrived and the need to override 
> function and object definitions at will is understood, but I wonder if 
> there is any language feature that can protect against accidental 
> overrides. 

Don't know.  I've assumed not; I'm not used to that sort of luxury, so 
it never occured to me to look for it ;-).  And it helps to remember
the programmer's mantra - "if it won't let you do the stupid things,
it won't let you do the clever things".  Sometimes, masking is deliberate,
and useful.

Most of the conflict you're describing seems pretty well serviced by 
the convention of picking names carefully, and sometimes adding the 
package name or initials to functions in the case of common names.


Indigo Industrial Controls Ltd.
jasont at indigoindustrial.co.nz
r-help 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-help-request at stat.math.ethz.ch

More information about the R-help mailing list