National Park Service

Inventory & Monitoring (I&M)

Installation & Configuration Notes for R

Please direct questions and comments about these pages, and the R-project in general, to Dr. Tom Philippi.

This page explains the simplest download and installation with my recommendations, and how to update to new versions of R. Separate pages provide information on advanced installation of related tools (ggobi for interactive visualization, gdal for advanced reading and writing of GIS file formats, DCOM for embedding R objects in MS Word or Excel), and installing on a Mac. Also, if you want screen by screen instructions, I recommend skimming this web page, then using Paul Geissler's page}


Downloading R

  1. Start at http://cran.r-project.org: Select your operating system under "Download and Install R".
  2. On the next page: Select base and follow link to download R. Note that for MSwindows, the installer can install both 32-bit and 64-bit versions of R.
  3. FAQs for R can be found under Documentation in the left navigation panel: Select FAQs, and open the relevant FAQ.
  4. Do not download R contributed packages at this time.
  5. Download the R-2.15.1.exe (or current version) file from CRAN, then run the file when downloaded.

Running Setup

The defaults are ok, but I have a few suggestions:

  1. For MSwindows, Install in c:/R/ not in c:/Program Files/R/ (Make sure you have full write permissions to c:/R and subdirectories.)
    • Spaces in directory paths, like spaces in file names, can occasionally bite you. The core of R and most contributed packages can handle spaces embedded in file names under MSwin, but some contributed packages from unix users don't bother. It is a good idea to keep spaces and special characters such as most symbols other than dash and underscore out of directory and file names. Also, agency IT folk often lock down the c:\Program Files directory so users can't write to it. Whenever you download and install a new contributed package, it will need to write files to directories beneath R/. You don't want to rely on your IT support every time you install a new package, and they don't want to have to handle those helpdesk calls.
  2. Select Components: add docs for packages grid and Matrix
    • When selecting components, add the technical manuals and pdf help pages.
  3. "Startup options: Do you want to customize the startup options?"
    • Choose 'Yes (customized startup)'
  4. "Display Mode: Do you prefer the MDI or SDI interface?"
    • For most uses you will want SDI, which creates a separate window for each interface component (e.g., separate console, graphics window, possibly help window, etc.). SDI is required for the Rcommander GUI, Tinn-R editor/GUI, and other packages using tcl/tk. When I ran R on a laptop with a small display, MDI can be useful to keep all R windows within a single large window, so you can minimize the containing window to clear all of them off the desktop temporarily. This is an option you can easily change later by editing the etc/Rconsole file and switching the commenting (#) between the MDI = yes and MDI = no lines. [This option may be dropped from the latest installer.]
  5. "Help Style: Which form of help display do you prefer?"
    • I choose html help so I can bookmark, open multiple tabs, etc.
  6. Leave everything else as default, and finish installation.
    • Either go with the defaults or choose the following (note that not all of these options appear in the 2.13.0 installer)
      • Standard internet access
      • Start menu shortcut in R
      • Create quicklauch icon if desired

⇑ To Top of Page

Tinn-R editor

You will want a good text editor when writing R code. Word processors such as MS word or OpenOffice writer are very poor choices: they save files with non-ACSII content, formatting, etc.

You can use NotePad or WordPad, which come with MS windows. However, several other text editors are more powerful, and colorize syntax, handle indentation, etc. The most commonly used editor may be Tinn-R, which integrates with the R command line window. Tinn-R is available from SourceForge at:

http://sourceforge.net/projects/tinn-r/

While the default is to install in c:\Tinn-R, I try to keep my root directory cleaner and install in c:\R\tinn-R instead. Pay attention to the "associate files" window. You probably don't want to associate .txt files with tinn-R, you might not want to associate any files other than *.R and *.tps.

I use TextPad, because I have used it for editing text data files for over a decade, and I'm an old dog. TextPad has syntax definitions for R (as well as a few hundred other scripts). If you use a Mac, there are several text editors build for writing scripts that have syntax definitions for R or S. The power user tool is probably EMACS with ESS (Emacs Speaks Statistics), but a quick search will find alternatives that are less cryptic.

Editors are a matter of personal preference, and sometimes personal pride; see xkcd:

Real programmers

⇑ To Top of Page

Installing Packages

There is no need to download all of the contributed packages you think you will need while you are performing the initial installation process unless there are firewall issues involved. Both the R GUI and Rcmdr include menu items that let you choose from a pull-down list of packages, and then automatically download and install the selected packages from a CRAN mirror. The etc/Rprofile.site file has a place to define a default CRAN mirror, or the pulldown menus let you choose among cryptically-named mirrors.

  1. Run R [If you have an IT person with admin permissions doing your installation, have them do this step].
  2. Installing Packages
    1. From the menu, select "packages", then install packages. If prompted: choose a mirror close to you. A list of packages will appear: Control-click to select more than one.
    2. Alternative: Instead of selecting packages from the menu, you can copy and paste the following lines into the R Console window at the command prompt (see the packages section below for more sets you may want to install):
      • install.packages(c("car", "conf.design", "corrgram", "DAAG",
        "effects", "ellipse", "faraway", "gplots", "lattice",
        "reshape", "plyr", "gdata", "leaps", "nlme", "lme4", "lmtest",
        "MASS", "Rcmdr", "latticedl", "RcmdrPlugin.HH",
        "RODBC","foreign", "reshape", "plyr", "gdata",
        "sciplot", "tree", "rggobi","R2wd"), dep=TRUE)
      • * Note: What is labeled "MASS(VR)" in the pull-down is named "MASS" in install.packages.
    3. Recommended Alternative: Download my file Install.R (remove the .txt from the file name), then edit your local copy with a text editor (textpad, notepad, etc., not MSword or OpenOffice Writer). Look at each block, and remove the '#' that comments out the lines with install.packages() and packages you want to install, for example, changing this:
      • ##############################################
        ######## Survey design & analysis (spsurvey does GRTS)
        # install.packages(c("ars","survey","spsurvey","RSurvey","memisc"),dep=TRUE)
      • #analysis of panel designs
        # install.packages(c("pcse","plm"),dep=TRUE)
      • to this:
      • ##############################################
        ######## Survey design & analysis (spsurvey does GRTS)
        install.packages(c("ars","survey","spsurvey","RSurvey","memisc"),dep=TRUE)
      • #analysis of panel designs
        install.packages(c("pcse","plm"),dep=TRUE)
      • Then, copy and paste the block into the R > prompt.
  3. Exit R via File | Exit in the menu at the top, or by typing into the command line:
  4. q()

⇑ To Top of Page

Additional Comments

Often the biggest difficulty is finding the packages that do what you need. If your need is ecological, a good place to start is the CRAN Task View for Environmetrics, an attempt to list common ecological analyses and the R packages available for each. [One obvious omission is mark/recapture analyses.] There are 25 task views for various fields. If that doesn't help, you can go to the main CRAN page for contributed packages and use search in your browser to search for key words for your topic.


Documentation

There are a large number of pdf documents available from CRAN at:

Note that the docs folder in your R installation directory (e.g., c:/R/R-2.13.0/docs) has a manual subdirectory with pdf copies of the main R manuals available from the first of these links. I recommend creating a directory such as c:\R\docs (distinct from c:/R/R-2.13.0/docs) and downloading documents you are most likely to need, as those documents don't change with new versions of R. For more information, follow the "Good References" link in the navigation panel on the left.

⇑ To Top of Page

Configuring R

Like many well-behaved programs, R allows configuration options to be set in several ways: OS environmental variables can be set. Global options that apply to all users and all instances of R can be set in the etc/Rprofile.site file. Project or user specific configurations may be set in .Renviron and .Rprofile files in the home directory.

The most useful system environmental variables are:

  • R_USER overrides the default to "My Documents" for user files
  • R_HOME overrides the default to "My Documents" for user files
  • R_LIBS points to an alternate location for installed packages (for when you can't obtain write permission to c:\R\R-2.13.0/library) Set R_LIBS=d:\R\library if you have write access to d:\R.
  • R_DEFAULT_PACKAGES sets the list of packages you want automatically loaded at startup in R.

The .Rprofile file allows you to set different options for different projects. You can put a .Rprofile file in your home directory that will apply to all of your R sessions, or separate .Rprofile files in each project subdirectory to have project-specific configurations.


Start-up Libraries

Certain libraries are loaded every time RGui is started. The following steps explain how to launch libraries on the start of RGui.

  1. Open: c:/R/R-2.13.1/etc/Rprofile.site, using NotePad, TextPad or any other text editor other than MSword.
  2. Append the following to the bottom of the file (making sure that the lines break exactly like this example):
    • local({
      old <- getOption("defaultPackages")
      options(defaultPackages = c(old, "car", "RODBC", "foreign", "DAAG", "MASS", "lattice ", "latticedl", "sciplot", "tree", "lme4"))
      })
  3. If you want the Rcmdr GUI to start each time you start R, append the following to the bottom of the file:
    • local({
      old <- getOption("defaultPackages")
      options(defaultPackages = c(old, "car", "RODBC", "DAAG", "MASS", "lattice", "latticedl", "sciplot", "tree","nlme", "lme4", "RcmdrPlugin.HH", "Rcmdr"))
      })
  4. Save Rprofile.site and exit the text editor.

You can grab Tom's old Rprofile.site here. (again, you have to delete the .txt appended to the file name).


Setting the CRAN Mirror

  1. Open: c:/R/R-2.13.0/etc/Rprofile.site, using NotePad, TextPad or any other text editor other than MSword.
  2. In lines following "set a CRAN mirror", replace my.local.cran with the url you selected from the list of mirrors.
  3. Remove the #'s in front of the lines following the "set a CRAN mirror" comment.
  4. Save Rprofile.site and exit the text editor.

⇑ To Top of Page

Updating to a New Version

New versions of R seem to come out right after we start our R course (2.8.2 in 2008, 2.10.0 in 2009, 2.12.0 in 2010). In my experience, it has never been the case that staying with the older version was better, but most new versions have little effect on what we use. Because new versions of base R usually require new versions of each package, brute force updating can be a pain. But, the developers behind R and the power users are lazy folks, so there are a couple of tools that make the process simpler. My directions are (substitute your old version for 2.9.2 and your new version for 2.10.0 in these):

  1. Create a new directory for the new version of R: c:/R/R-2.10.0
  2. Copy the old library directory to that new directory (c:/R/R-2.9.2/library > c:/R/R-2.10.0/library), and the old Rprofile.site file from c:/R/R-2.9.2/etc to c:/R.
  3. Uninstall the old version of R: Start menu > R > uninstall 2.9.2 is a bit faster than administrative tools > add/remove software
  4. Install the new version of R from cran to the new directory (c:/R/R-2.10.0) by selecting installation target c:/R.
  5. Copy Rprofile.site from where you stashed it to the new etc directory (from c:/R to c:/R/R-2.10.0/etc). You probably shouldn't do this for the update from 2.92 to 2.10.0, as the CHM option for the help system is no longer supported as of 2.10.0; instead, edit the Rprofile.site file the installer builds for you by adding the local mirror and the list of packages you want loaded at startup (see above).
  6. Start R, and paste the following command at the prompt:

update.packages(checkBuilt=TRUE, ask=FALSE)

Alternatively, step 6 can be replaced by starting the new version of R, then selecting update packages from the packages menu.

I have perhaps 50 packages installed, and the entire update process requires less than 5 minutes of my time, and then another 45 minutes of unattended running to download the updated packages (I have a slow internet connection at work).

⇑ To Top of Page

Last Updated: December 30, 2016 Contact Webmaster