[R] How to get the namespace of a function?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Feb 3 14:08:25 CET 2006


Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:

> On Thu, 2 Feb 2006, Fernando Saldanha wrote:
> 
> > I am trying to imitate "encapsulation" from other languages like Java
> > or C++. Coming from that background, it bothers me that I can commit
> > errors like the following:
> >
> >> x <- 1
> >> f <- function(z) { y <- x; y + 1 } # Mistake: I should have written y <- z
> >> f(10)
> > [1] 2
> >
> > In a language like Java the interpreter would have noticed that x was
> > an undeclared variable and an error message would be issued. R, on the
> > other hand, allows the code to run, as x exists in the global
> > environment. I was trying to avoid such situations by  setting the
> > environment of f to be NULL. If there is a better way to catch this
> > type of errors I would be interested in knowing about it.
> 
> The codetools package.  See http://www.stat.uiowa.edu/~luke/R/codetools.
> 
> (They are not R errors, but the codetools package can tell you that 'x' is 
> not necessarily in scope.)

Yep, and nor is "<-", "+", and "{"... 

(Does codetools actually check those? Probably not, but it goes to
show why full encapsulation is hard or impossible to achieve in a
language where "everything", including functions and operators, are
objects.)

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list