[R] how to run a script at the beginning of an interacive session ?

Marc Schwartz MSchwartz at mn.rr.com
Wed Jun 8 05:05:31 CEST 2005


On Tue, 2005-06-07 at 19:37 -0700, Mike R wrote:
> Hi All,
> 
> How does one create an executable R-script, similar
> to an executable python script, or shell script except 
> that when the R-script is finished, the R session remains
> open and becomes interactive.
> 
> If I do this:
> 
>   R < script.r
> 
> then R exits when finished.
> 
> On the other hand, starting R then typing 
> 
>    source("script.r",echo=T)
> 
> is becoming tedious.
> 
> Thanks in advance.
> 
> Mike

See ?Startup, specifically the use of the .First function in .Rprofile.
Note importantly that .First is run before any packages are loaded,
including the base packages.

As an example, if you wanted to create a plot() on startup each time,
create a file called "script.r" containing:

library(graphics)
plot(1:10)


Then in .Rprofile, create an entry as follows:

.First <- function() {source("script.r")}

With this in place, the plot will get created each time you start R.

HTH,

Marc Schwartz




More information about the R-help mailing list