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

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Feb 3 08:30:11 CET 2006


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.)

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list