The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Kickstarting R
Enhancing your startup experience: The .First file

What happens .First?

One of the best things about R is that you can change many of its characteristics to suit you. You will probably find a number of these things that you want to do every time you start R. These can all be conveniently packaged in a function named .First() which R will run automatically when you start it.

As of this revision, the .First() function is defined in the Rprofile file (see below) that is also run on startup. .First() is a great place to start learning about functions in R.

You can write a function on the command line, but it is a lot easier using a text editor. See what the .First() function contains after you have started R by entering:

> .First
function() {
    require("ctest", quietly = TRUE)
}

Bit of a catch-22 here, as you want to edit the function without having changed the editor to your preference. One way to get around this is to edit the Rprofile file with your favorite editor. You may be unable to do this if you don't have the authority to change files in the R directory tree, or you may be unwilling to subject other people who use R to your favorite editor. To stay on the safe side, you'll have to create a .First() file in the current directory that will clobber the one defined in Rprofile, but perform the same functions.

> .First<-function() {
+ require("ctest", quietly = TRUE)
+ options(editor="my_favorite_editor")
+ }

on the command line. If you enter just the name of the function as above, you should see the results of your creation. Now add the empty parentheses,

> .First()

and you will run the function manually, changing the editor to whatever you have chosen. You can change options, invoke functions like x11() that will start up a graphics window, load a library or anything else you want to do to set up your initial R environment. Allow me a moment of pretense and assume that you would like to have the xtab() function available when you are using R. Simply add the following line to your .First() function, changing the directory path to the one appropriate for your system.

source("/home/jim/R/kickstart/xtab.R")

You will have to copy the .First() function to any directories in which you want to start R so that it will be available at startup (but see below for more far-reaching initializations). One easy way to do this is to have a text file containing the current version of .First() somewhere. When you start working in a new directory,

> source("/home/jim/R/First.txt")

and remember to save the workspace image when you leave R.

.First can even reel off a sound file of atmospheric music if it makes you feel better:

system("noatun /home/jim/mod/toonz/r_intro.wav")

but of course you'll have to supply your own intro music and player.

The hierarchy of initialization files

The .First() function will only be run if it is in the .RData file in the directory in which R starts up. Above the .First() function are a few other ways to customize the R startup.

The preferred method at the moment appears to be editing the $R_HOME/etc/Renviron file for globally setting a default or the .Renviron file that is created in your home directory for a shared system. For example:

...
## Default browser
R_BROWSER=${R_BROWSER-'/usr/bin/konqueror'}
## Default editor
EDITOR=${EDITOR-${nedit}}
...

will change the default Web browser to Konqueror and the default editor to NEdit.

The .Rprofile file may be in your home directory, or may be in R working directories. It will be read by the R program when it starts up. You can edit this file and its global counterpart $R_HOME/library/base/R/Rprofile in much the same way.

For more information, see An Introduction to R: Customizing the environment.

Back to Table of Contents

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.