Install and configure R for your system

Before you start

Assuming R has been successfully installed on your computer:

Download, install and configure R

R can be downloaded via the R website: http://www.r-project.org/. The repository for the R program, associated packages and documents is a facility called `CRAN' (Comprehensive R Area Network) which is essentially a set of websites that provide a mirror of the main R website. Click on the `CRAN mirror' link in the menu on the left side of the home page. Select a location for downloading, preferably a location near to you. If using Windows, click on the R-installation .exe file to install.

Packages

Packages (also known as libraries) extend the functionality of R. Packages can be downloaded from the R website (use the links provided below). Once downloaded as zip files you need to install them into your copy of R. Once installed each package needs to then be loaded into each R session before it can be used. It is probably best to download and install packages as they are required. That said, we find the following packages particularly useful:

Package Description
car John Fox's companion to applied regression
epiR Functions for the analysis of epidemiological data
geoR Functions for geostatistical analyses
Hmisc A set of miscellaneous tools for regression analysis
lattice Functions for trellis plotting
maptools A set of functions for handling spatial data
RODBC ODBC connectivity
RColorBrewer Colour palettes
spdep A set of functions for handling spatial data
spatstat A package for analysing spatial data

To install a package, select -PACKAGES -INSTALL PACKAGE(S) FROM LOCAL ZIP FILES. Browse to locate the zip files you wish to install. If you want to install a number of packages at once, use the CTL key to select each package. Once selected, click open. A message will be displayed to confirm that the selected package has been succssfully installed. R packages are in a constant state of development, and for this reason it is important that you are able to update them on a regular basis. There are two options for doing this. The first is to check on the R website for updates. The second (preferred) option is to configure R to establish a connection to CRAN when asked and to download only those packages where a later version exists. To set this facility up when you're operating within an institution that has a firewall, edit the program shortcut to:

"C:\Program Files\R\R-2.14.1\bin\i386\Rgui.exe" http_proxy=http://tur-cache2.massey.ac.nz:8080 http_proxy_user=ask

This will result in the program asking for a user name and password when the `UPDATE PACKAGES FROM CRAN' option is selected from the menu bar. An alternative method for installing packages is to use the install.packages command:

install.packages("ggplot2", dependencies = TRUE, repos = "http://cran.stat.auckland.ac.nz")

The advantage of this method is that by setting the dependencies argument to TRUE R will download all of the additional, dependent packages required. Use the following code if you want to: (1) list all of the packages installed on your system, and (2) download each of the listed packages:

mypack <- installed.packages()
mypack <- mypack[,1]

for(i in 1:length(mypack)){
   download.packages(mypack[i], destdir = "D:\\Software\\Statistics\\R\\packages", type = "win.binary")
}

GUI preferences

To edit the default visual appearance of the graphic user interface, select -EDIT -GUI PREFERENCES. Set the pager rows to 25 and the pager columns to 80. If you intend to use Tinn-R as your editor, make sure the SDI tick box is selected. Save your settings in a file called Rconsole by clicking `Apply' then `Save'.

\TeX \begin{figure}[h]
\centering
\includegraphics[width=17.32cm]{../../images/Rgui-02.png}
\caption{R graphic user interface settings.}
\label{fig:rgui-02}
\end{figure}

Figure 1: R graphic user interface settings.

Library locations

Sometimes (particularly on corporate machines) user access is restricted to C:\PROGRAM FILES folder. For this reason, you may wish to install packages to another (unrestricted) folder. To point R in the direction of this folder (say, for argument's sake it is D:\TEMP) run the .libPaths command before loading libraries in your R session:

.libPaths("D:\\TEMP")
library(epiR); library(geoR)

Miscellaneous

To stop the program from asking if you want to save the workspace when the program terminates edit the program shortcut to:

"C:\Program Files\R\R-2.14.1\bin\i386\Rgui.exe" --no-save

You can of course have multiple preference settings in the one shortcut. For example:

"C:\Program Files\R\R-2.14.1\bin\i386\Rgui.exe" http_proxy=http://tur-cache2.massey.ac.nz:8080 http_proxy_user=ask --no-save

Upgrading R

Method 1. Install package installr and run the following code from the R GUI (not R Studio).

library(installr)
updateR()

Edit the system PATH variable to point to new version of the program.

Method 2.

  1. Make a copy of Rprofile.site. Uninstall the old version of R.
  2. Install the new version of R --- select option to install PDF reference manual. Copy installed packages from the old version to the library folder in the new installation.
  3. Edit program shortcut as described above: "C:\Program Files\R\R-2.15.2\bin\Rgui.exe" http_proxy=http://tur-cache1.massey.ac.nz:8080 http_proxy_user=ask --no-save
  4. Set GUI preferences: console rows 35 and console columns 125. Save settings to C:\Program Files\R\R-2.10.0\etc\Rconsole.
  5. Copy the Rprofile.site file from previous installation to C:\Program Files\R\R-2.10.0\etc folder.
  6. Edit the program shortcut to set D:\TEMP as the Start in folder.
  7. Run update.packages(checkBuilt = TRUE, ask = FALSE) in the new R and then delete anything left of the old installation.
  8. Edit the system PATH variable to point to new version of the program.

Tinn-R

The installation file for Tinn-R can be downloaded from the Tinn-R website: http://www.sciviews.org/Tinn-R. A suggested configuration for Tinn-R is as follows:

Select -R -HOT KEYS OF R. Highlight the white area adjacent to `Send: selection (echo=TRUE)' and in the box titled `Set (hotkey)' click the F6 button. Then click `Add'. Finally, make sure the `Active' tick box is selected at the bottom of the form. This facility allows you to select a section of code in Tinn-R and then to pass it to R for processing.

\TeX \begin{figure}[h]
\centering
\includegraphics[width=17.32cm]{../../images/Tinn-R_hotkeys.png}
\caption{Hotkey settings in Tinn-R.}
\label{fig:tinn-r_hotkeys}
\end{figure}

Figure 2: Hotkey settings in Tinn-R.

So a routine startup for R -- Tinn-R is as follows:

You can control the focus once you have submitted a line of code (i.e. do you want the cursor to remain in R once a line of code has been submitted, or do you want it to return to Tinn-R) by clicking -OPTIONS -RETURN FOCUS TO EDITOR (AFTER SEND/CONTROL RGUI).

Getting help

You can get help on any specific function while running R by typing help(function), ?function, or by asking for an example with example(function).

If you really can't get by without a graphical point and click interface, and coding terrifies you, there is an option. The package R Commander, provides a point and click interface for R. It's not very powerful, but it can yield up some code snippets for you to use later on.

The R website is a great resource. The program manual is at http://cran.r-project.org/manuals.html and you can find useful tutorials at http://cran.r-project.org/other-docs.html.

The searchable archives of the R mailing list are at: http://tolstoy.newcastle.edu.au/~rking/R/.

A set of R tips are at: http://www.ku.edu/~pauljohn/R/Rtips.html.

The R graph gallery is really worth a look: http://addictedtor.free.fr/graphiques/index.php.