[R] R package: Where to put code to Run Once Only?

Martin Morgan mtmorgan at fhcrc.org
Mon Apr 6 16:51:43 CEST 2009


Hi Ken --

Ken-JP wrote:
> 
> Is there a specific place where we can place code to run once only in a
> package?
> 
> I have code that switches based on Sys.info()[["nodename"]] - but since this
> just about never changes, I would like to run it only once when someone
> runs:
> 
> require( mypackage ) or library( vte )
> 
> I'm tempted to have some free-floating code in global space (in one of my
> package's .R files), but it just seems so wrong/hacky to do it this way.
> 
> Where is the proper place to put this code?

in a function named .onLoad (or, rarely, .onAttach) when your package
has a name space, or .First.lib when it does not. See the help pages for
each.

Martin

> - Ken
> 
> 
>




More information about the R-help mailing list