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( mypackage ) 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? - Ken -- View this message in context: http://www.nabble.com/R-package%3A-Where-to-put-code-to-Run-Once-Only--tp22908592p22908592.html Sent from the R help mailing list archive at Nabble.com.
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?That fine, in principle, convention is to have code in zzz.R, see Writing R Extzensions. Uwe Ligges> - Ken > > >
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 > > >
Uwe Ligges <ligges <at> statistik.tu-dortmund.de> writes:> That fine, in principle, convention is to have code in zzz.R, see > Writing R Extzensions.Finally I got it, zzz comes from Extzensions. In the fields I normally work in, zzz mean the LAST thing to do before going to sleep or to crash the system. Dieter
Dieter Menne wrote:> Uwe Ligges <ligges <at> statistik.tu-dortmund.de> writes: > >> That fine, in principle, convention is to have code in zzz.R, see >> Writing R Extzensions. > > Finally I got it, zzz comes from Extzensions. In the fields I normally work in, > zzz mean the LAST thing to do before going to sleep or to crash the system.Dieter, in fact the code in zzz.R is processed if zzz.R will be processed according to sorting in a C locale. But since all other code regularly consists of function definition or definition of methods and therelike, the ordering is irrelevant (and if your code to be processed dierectly depends on functions defined in your package, then it becomes relevant not to happen in the very beginning. Best, Uwe> Dieter > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.