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

Mark.Bravington at csiro.au Mark.Bravington at csiro.au
Wed Apr 14 01:17:56 CEST 2004


I think you can already do this via .First(). If you start your .First with something like this [give or take some parentheses]

if( !is.null( runfirst <- Sys.getenv( 'RUNFIRST')))
  try( eval( parse( text=runfirst))))

then whatever you pass in as RUNFIRST=... should get executed on startup. Could even be a call to source(...) if you want to source a particular file. I have a similar switch at the start of my own .First, which bypasses the rest of the function if the NOFIRST variable is set-- handy for debugging .First itself.

Admittedly, for this to work you must actually *have* a .First in a .Rdata file, but .First is so useful that surely everyone has one :) ?

[And I reiterate the suggestion that .First should run immediately before giving control to the user-- perhaps unlike .Rprofile-- rather than before packages are loaded.]

As a general comment-- not just on this case-- I think there are dangers in allowing R to accrue too many new alternative ways of doing the same thing. My own experience is that "or you can do it this way" improvements in usability tend to be minor, and are more than outweighed by making R harder to learn-- and also harder to teach-- because people get mixed messages. In this specific case, what with .Rprofile, .First, .First.sys, environment variables, autoload lists, hooks, .onLoad, .onAttach, .First.lib, etc, there are already heaps of places to put "extra stuff"-- some might think too many. Just my 2c's worth, anyway

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 

#-----Original Message-----
#From: r-devel-bounces at stat.math.ethz.ch
#[mailto:r-devel-bounces at stat.math.ethz.ch]On Behalf Of Duncan Murdoch
#Sent: Wednesday, 14 April 2004 1:32 AM
#To: Erich Neuwirth
#Cc: r-devel at stat.math.ethz.ch
#Subject: [Rd] Re: [R] Execute function at startup
#
#
#As far as I know what you're asking for doesn't exist, so I've moved
#this discussion to r-devel.
#
#On Tue, 13 Apr 2004 16:22:37 +0200, Erich Neuwirth
#<erich.neuwirth at univie.ac.at> wrote :
#
#>It would be convenient to have something like
#>Rgui runfist="myfunction()"
#>in Windows.
#
#That looks as though you're creating an environment variable named
#"runfist".  I'd rather use some different syntax than that...
#
#>The reason:
#>AFAIK Rgui does not accept piped input
#>(RGui < myfile.R does not seem to work).
#
#That would be hard to implement, as most people start Rgui using a
#shortcut, and shortcuts don't support I/O redirection.
#
#However, I think it would be useful to be able to source a file
#somehow.  I can see a couple of different possibilities:
#
#Rgui --exec source('myfile.R')
#
#or 
#
#Rgui --source myfile.R
#
#The semantics would be that the code is executed after all other
#initialization is done (profile executed, user's saved image restored,
#etc.), just as if it had been typed in the console.  When it's done,
#control returns to the console (unless there's a q() in the commands).
#
#>A solution could be to put a few fuctions in Rprofile and then
#>give the name for one of these functions to be executed at startup as
#>a command line parameter to Rgui.
#>
#>Can something like this be done?
#
#Not now, but I think we should have something like this for 2.0.0.
#There's already been some discussion about whether the bug fix to
#.Profile is desirable (it's now run before most packages are
#attached).  It makes sense to me to have a way of running code there,
#and also to have a way to run code just before the system gives
#control to the user.  
#
#Duncan Murdoch
#
#______________________________________________
#R-devel at stat.math.ethz.ch mailing list
#https://www.stat.math.ethz.ch/mailman/listinfo/r-devel
#



More information about the R-devel mailing list