[R] namespace? environment? how to manage functions?

Duncan Murdoch murdoch.duncan at gmail.com
Thu Jul 22 15:10:34 CEST 2010


On 22/07/2010 8:58 AM, Michael Friendly wrote:
> In http://tolstoy.newcastle.edu.au/R/e7/help/09/06/1900.html
> Jim Holtman wrote:
>
> Here is the way that I do it instead of creating a package of my 
> functions. I 'source' the file into an environment and then attach the 
> environment to keep the global from being clustered:
>
> # read my functions into a environment
> .my.env <- new.env()
> sys.source('c:/perf/bin/perfmon.r', envir=.my.env) attach(.my.env)
>
> Is there a way to do this directly in .Rprofile without sourcing from a 
> separate file?  I've looked at
> sys.source, but don't understand how it works.
>
>   

You mean you want all your definitions in the Rprofile file?  The way to 
do that is to use local().  That is,

.my.env <- local({
   f <- function(x) x^2
   # all your other definitions
   environment()  # Put this as the last line, to return the environment 
that local() created, where
   # all your definitions now live
})
attach(.my.env)

Duncan Murdoch



More information about the R-help mailing list