[R] a question about scoping functions

Duncan Murdoch dmurdoch at pair.com
Sat Mar 27 14:31:15 CET 2004


On Fri, 26 Mar 2004 21:28:08 -0500, Rajarshi Guha <rxg218 at psu.edu>
wrote :

>Is there anyway to take the functions helper1 and helper2 outside func
>(but in the same file) yet keep them from showing up in the global
>namespace when the source file for func() is loaded?

A simpler (but slightly less effective way) than what Andy suggested
is to name the helper functions with names starting with a dot.  By
default ls() won't show objects like that (just like ls in Unix).
E.g.

> visible <- 1
> .notvisible <- 2
> ls()
[1] "visible"

>A related question is: say I move helper1 and helper2 to a file called
>helper.R
>
>When I want to use the helper functions inside func() I can do
>source('helper.R') - but that would place them in the global namespace.
>Is it possible to source some file within a func() such that the sourced
>functions will be local to func()?

Putting the line

  source('helper.R', local = TRUE)

in func() will do that.

Duncan




More information about the R-help mailing list