Chapter 2 Installation
R is open-sourced and can be installed on operating systems such as Windows, OS X, or many other Linux/Unix distributions. This chapter describes the recommended way to install R and R packages across different platform.
2.1 R Installation
2.1.1 Windows
The best way to install R on a Windows PC is to download the binary executable from CRAN. Click through “Download R for Windows” and “install R for the first time” to get the file. For example, the latest stable version is R-3.2.4revised-win.exe when this chapter was written.
One can also find older versions of R under the CRAN’s /bin/windows/base/old folder.
The installation of R for Windows from the above executable file will also include a GUI tool called RGui.exe
for interactive development purpose.
2.1.2 OS X (Mac)
The best way to install R on a Mac is to download the binary package file from CRAN. Click through “Download R for (Mac) OS X” and download the latest stable version. For example, the latest stable version is R-3.2.4.pkg when this chapter was written.
One can also find older versions of R under the CRAN’s /bin/macosx/base/old folder.
The installation of R for Mac from the above pacakge file will also include a GUI tool called R.app
for interactive development purpose.
2.1.3 Ubuntu
There are many ways to get R installed on a Linux box. Here we use Ubuntu as an example.
Naturally one would probably like to check if there is already available compiled package from any repository of your distribution. To do so, use sudo apt-cache policy r-base
and something like the following may return:
r-base:
Installed: (none)
Candidate: 3.0.2-1ubuntu1
Version table:
3.0.2-1ubuntu1 0
500 http://mirrors.digitalocean.com/ubuntu/ trusty/universe amd64 Packages
But generally the compiled version on Ubuntu default repository won’t catch up with the latest one on CRAN. If it is okay to install the older version of R then all the things left to do is a simple sudo apt-get install r-base
.
However, if one would like to install the latest stable version of R, a mirror of CRAN must be setup. This is documented in details under CRAN’s bin/linux/ubuntu/ folder.
For Ubuntu 14.04, add deb https://<my.favorite.cran.mirror>/bin/linux/ubuntu trusty/
to /etc/apt/sources.list
, where <my.favorite.cran.mirror>
should be replaced with one of the preferred mirrors from CRAN’s mirror list. One choice is the NTU mirror so the added line to the sources.list file will be:
deb https://http://cran.csie.ntu.edu.tw//bin/linux/ubuntu trusty/
After this is done, use sudo apt-get update
to update the package manager and use sudo apt-get install r-base
to install the latest version of R. You may also like to do sudo apt-get install r-base-dev
to further install developer’s tools.
If there is any error concerning the secure key issue, consult the “Secure APT” section for solution.
2.1.4 Update the R Installation
It is recommended to directly install the newer version of R, if wanted. Each version of R is separately maintained in the installed system. Of course the newer R will come with only built-in packages. It is recommended that users re-install all the packages wanted from the newer R.
2.2 R Package Installation
2.2.1 From the Internet
2.2.1.1 CRAN
The most convenient way to install R package is to use the install.packages
function. By default it will install the specified package from CRAN: The Comprehensive R Archive Network. For Mac and Windows users usually the desired package is already compiled and can be readily install in seconds. For Linux users the install.packages
still works but the package will be compiled from its source code.
For example, to install the powerful data.table
package:
install.packages("data.table")
The function automatically resolve dependency issue, so any dependent packages will also be installed, if any. One can also uninstall a package by using the function remove.packages
.
2.2.1.2 GitHub
Not all packages are available at CRAN. And there are more and more developers prefer to host their source codes on platform like GitHub. Sometimes a package can be found in both CRAN and GitHub but the latest developing version is only available at GitHub.
To directly install a package from its GitHub repository, use the function devtools::install_github
. One must first install the package devtools
. For Windows users, additional dependency on the Rtools is required before installing devtools
. For Mac users, the XCode command line tool may be required. Firstly install XCode; then open Terminal.app and type xcode-select --install
.
To install package from GitHub, the name for the repo must be specified. For example, to install the package ArgParser
from the book author’s repo:
devtools::install_github("everdark/ArgParser")
2.2.2 From Local Source or Binary
Source codes of R packages is stored as tar.gz
. Binaries are stored as .zip
for Windows and .tgz
for Mac OS X. It may happens that users want to directly install the package from these files locally. This can also be achieved by install.packages
, by specifying argument repos=NULL
and point pkgs
to the full path of those files. For example:
install.packages("ggplot2_2.1.0.zip", repos=NULL)