R Setup

Installing and updating R

Design of the R system

The R system is divided into two conceptual parts:

  1. The “base” R system that you download from CRAN.
  2. Everything else.

R functionality is divided into a number of packages. The “base” R system contains, among other things, the base package which is required to run R and contains the most fundamental functions. The “base” system contains also some other packages. Furthermore, every R installation contains “recommended” packages, that are not necessarily maintained by R Core.

“Everything else” point out CRAN “contributed” packages and packages that are not on CRAN. This does not mean that these packages are necessarily of lesser quality than the above, e.g., many contributed packages on CRAN are written and maintained by R Core members. The goal is simply to try to keep the base distribution as lean as possible. Beyond CRAN, many packages are avalaible in BioConductor project or in Github repositories.

Installing R

R is available for Windows, Mac Os and Linux.

The base R can be downloaded from the Comprehensive R Archive Network (CRAN) website. The CRAN is a collection of sites which carry identical material, consisting of the R distribution(s), the contributed extensions, documentation for R, and binaries.

Go to www.r-project.org. Here you can read about R and see what’s new in the latest version.

Screenshot of the R-project website

Click on CRAN under Download in the left list (see Figure Screenshot of the R-project website). That’ll take you to a list of servers (mirrors) in different countries where you can download R.

Now you can choose what operative system you have. Choose within the upper box with “Download and Install R”. The box contains “Precompiled binary distributions”; sounds complicated, but means that the program is ready to use, with installation program and all.

Windows users click on “base” and then click “Download R X.XX.X for Windows”.

Mac users click on “R-X.XX.X.pkg (latest version)”.

Linux users find download files and installation instructions for their Linux distribution in the website.

X.XX.X identify the current version of R. In April 2012, the current version is the 2.15.0.

To install R, Windows and Mac users must just double click on the file and follow the installation instructions.

Linux users can download and install R using the precompiled R binary for their distribution. Alternatively, experienced Linux users can compile R from sources. Currently, precompiled R binary are available for Debian, Ubuntu, Suse and RedHat. The R installation package for RedHat downloadable from CRAN is obsolete. An R updated version for Red Hat Enterprise Linux 5 is available for free by Revolution Analytics.

The Figure Screenshot of a first try with R shows a first try with R, just after installation.

Screenshot of a first try with R

Installing R packages

R functions are collected in packages. Packages that are not contained in the “base” R systems can be downloaded from the CRAN website. A list of R packages accompanied by a brief description can be found on the CRAN website where there are more than 3700 packages available. Many of these packages are very useful; however, there are some packages in prerelease, incomplete packages, “abandoned” packages (i.e. not more updated) and/or packages containing functions with errors or compatibility troubles.

The simple way to install an R package is through the install.packages() function, directly from R. In Linux, R must be executed as administrator to install a package. Installation must be executed before the first use of a library.

When a function of a package that is not contained in the “base” R systems is required than the package must be loaded. The library(pkg) function load the pkg package.

Updating R

In Windows and Mac, there is not an automatic way to update R. Two or more versions of R can coexist in the same machine, so a newer release of R can be installed beside the old release. Unfortunately, packages must be re-installed in the “new” R version. A copy-and-paste of package files from the directory containing the packages of the “old” R version to the directory containing the packages of the “new” R version may be useful when many packages are installed. Within R, the .Library variable shows the directory containing the packages. After the copy-and-paste, packages of the “new” R version should be updated.

In Linux, usually one R version at time can be installed. R can be updated following the instructions in the CRAN website.

Updating R packages

R packages can be updated typing update.packages() within R. In Linux, R must be executed as administrator to update a package. Packages should be updated regularly.

Graphical User Interfaces

R default interfaces

R is provided with a Command Line Interface (CLI), which is the preferred user interface for power users because it allows direct control on calculations and it is flexible. However, good knowledge of the language is required. CLI is thus intimidating for beginners. The learning curve is typically longer than with a Graphical User Interface (GUI), although it is recognised that the effort is profitable and leads to better practice (finer understanding of the analysis, command easily saved and replayed).

R is available for many different operative systems so it does not provide the same graphical interface. When you use the R program interactively, it issues a prompt when it expects input commands. The default prompt is ‘>’. In Windows, RGui is the default GUI. Inside RGui there is the RConsole window.In Macintosh, RConsole is the default GUI. In Linux, R does not provide any graphical interface by default and it can be used through CLI.

R alternate interfaces

Several projects develop or offer the opportunity to develop alternate user interfaces. They are presented at www.sciviews.org/_rgui.

RStudio (www.rstudio.org) is a free and open source multi-platform integrated development environment (IDE) for R. It provides syntax highlighting, code completion and smart indentation. Moreover, it executes R code directly from the source editor and it manages easily multiple working directories using projects. It provides:

  • workspace browser and data viewer;
  • plot history, zooming, and flexible image and PDF export;
  • integrated R help and documentation;
  • Sweave authoring including one-click PDF preview;
  • searchable command history.

Similarly to RStudio, JGR (rforge.net/JGR) and Rkward (rkward.sourceforge.net) are multi-platform IDE. JGR is an R console replacement, it provides also a very decent R code editor with syntax highlighting, calltips, completion and submission of code to R. Rkward has interesting features too to edit R script files and submit code to R.

Tinn-R (www.sciviews.org/Tinn-R) is one of the most used R editor by Windows users. It includes R syntax highlighting, help on R functions syntax while you type them, direct submission of R code and many other tools to control R from within the editor.