[Rd] Re: [R] Execute function at startup

Mark.Bravington at csiro.au Mark.Bravington at csiro.au
Thu Apr 15 06:08:41 CEST 2004


#-----Original Message-----
#From: Duncan Murdoch [mailto:dmurdoch at pair.com]

#I forget whether Erich mentioned it in this post, but in other
#messages he's told me that the intention is to use this as a way to
#start R for naive users, immediately firing up Rcmdr for instance.
#It's possible to install a .First function for such users, but it
#would be easier if the capability was built in.

Fair enough-- I hadn't realized.

One sneaky possibility without changing R itself: how about writing a miniscule package that simply looks for a RUNFIRST environment variable and executes it? This functionality could either be in the R source file for the package, or in the .First.lib function (decisions, decisions...)-- there wouldn't be any other functions in the package. To use the functionality, the package would just be added to the default package list in the R_DEFAULT_PACKAGES env var. If people wanted to avoid a .Renviron file, R could be invoked via

R(GUI) R_DEFAULT_PACKAGES=c("stats","graphics","utils","methods","runfirst") RUNFIRST=<<whatever>>

In fact I suppose you could cheat even further by including 

library( stats); ... library( methods)

in the .First.lib of 'runfirst'-- then the invocation line (or icon "target") could be simplified to

R(GUI) R_DEFAULT_PACKAGES='runfirst' RUNFIRST=<<whatever>>

which is pretty "naive-friendly". In terms of Erich's 12/4 query about sourcing a file, the RUNFIRST could be %1 I guess.

Note that this should work even under the current policy of "autoloading last", because all standard packages would be availble before the RUNFIRST code gets executed (not the case with a .Rprofile file, unless precautions are taken). Might need to change the order of the packages in the first command line, to ensure utils etc. really are available.

This type of "solution" either underlines or undermines my point about proliferation of alternatives-- I'm not sure which... ;)

Mark

*******************************

Mark Bravington
CSIRO (CMIS)
PO Box 1538
Castray Esplanade
Hobart
TAS 7001

phone (61) 3 6232 5118
fax (61) 3 6232 5012
Mark.Bravington at csiro.au



More information about the R-devel mailing list