[R] a question about scoping functions

Liaw, Andy andy_liaw at merck.com
Sat Mar 27 03:38:50 CET 2004


Here's one possible alternative:

-  Write the helper functions outside of func().
-  source() the file containing all the functions.
-  save(helper1, helper2, file="helperfuns.rda")
-  rm(helper1, helper2)
-  attach("helperfuns.rda")

Eventually you may want to put things into a package...

HTH,
Andy

> From: Rajarshi Guha
> 
> Hi,
>  I've written some helper functions which are used by another function
> (called func()). When func() is sourced I dont want the 
> helper function
> to be seen in the global namespace (as shown by ls()).
> 
> Currently what I have done is:
> 
> func <- function() {
>   helper1 <- function() {
>     ...
>   }
>   helper2 <- function() {
>     ...
>   }
>   # some code
> }
> 
> 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 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()?
> 
> Thanks,
> -------------------------------------------------------------------
> Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net>
> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
> -------------------------------------------------------------------
> Did you hear that two rabbits escaped from the zoo and so far 
> they have
> only recaptured 116 of them?
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list