Installing and Using R

Installing R and Contributed Packages

Contributed Packages

There are many add-on R packages, contributed by the R communitiy, which are available at the CRAN website.

There are also some locally developed packages available from EOL:

  • eolts
  • eolsonde
  • isfs

The code for these packages is on github, at https://github.com/ncareol/R-eol, along with a wiki https://github.com/ncareol/R-eol/wiki.

Installing R in Mac OS

See http://cran.r-project.org/doc/manuals/r-release/R-admin.html#Installing-R-under-OS-X

  1. In order to use X11 graphics, first install xquartz. See http://xquartz.macosforge.org. One should also reinstall XQuartz after an OS upgrade.
  2. Download R for (Mac) OS
  3. Double-click on the R pkg file, agree to license
  4. If you have admin privileges, you can install R for all users

After installing, you should be able to run R.app from the Applications folder, or by entering "R" at the shell prompt in a terminal window

RStudio is available from http://www.rstudio.com/products/rstudio/download/.

Installing Contributed R Packages in Mac OS

Packages are usually available in binary form for Macs. These binary packages should be self-contained, and not require the installation of other software on your system.  If they contain C, C++ or Fortran code, they should be statically linked to any required libraries.  For example, a binary package that uses NetCDF routines should not require you to install the NetCDF library on your system.  

The easiest way to install packages on a Mac is from the R app.  From the top menu-bar of the R app, select:

  1. Packages & Data
  2. Package Installer
  3. Select the Cran (binaries) repository
  4. Check Binary Format Packages
  5. Enter a string to search for a package, such as "splus", or "quant"
  6. Press Get List
  7. Select one or more packages from the list (using command-click to select multiple packages). If a package is not listed, try selecting another mirror as discussed below under #CRAN mirror.
  8. For Install Location choose At System Level if you have admin privileges, or At User Level otherwise.
  9. Check Install Dependencies
  10. Install Selected
  11. After a successful install, the version number of the package(s) should be shown under Installed Version

You can also install packages from the R command prompt, as shown below, under RedHat Linux 

Local R Packages, eolts, eolsonde, isfs

eolts, eolsonde, and isfs are developed at EOL, and are available on an R repositiory here.

To view and install these packages with the Package Installer, select Other Repository, instead of CRAN (binary), and enter https://www.eol.ucar.edu/software/R in the text area on the right, then Get List and install as above. See below about listing the EOL R repository from a browser.

Binary versions of EOL packages are available for the following versions of Mac OS X and R.  Binaries built for 10.9 and 10.10 are not compatible with 10.8 and below. Currently we do not have a Mac running 10.8 where we can build binaries for the earlier versions of Mac OS X.

Supported versions of R and Mac OS in EOL Binary Packages
R MacOS Binary Packages 
3.3 10.12, Sierra yes
3.3 10.11, El Capitan yes
3.3 10.10, Yosemite yes
3.3 10.9, Mavericks yes
3.1, 3.2   old. out of date

Installing or Updating Packages with RStudio

RStudio also provides a GUI for installing and updating contributed packages. Under Tools, select Install Packages, or Check for Package Updates. If a package to be updated has been loaded with the library() function, RStudio will ask whether it should restart R. If that package is loaded at startup via an .Rprofile, or $(R RHOME)/etc/ Rprofile.site, the restart won't help. You should edit or temporarily rename your .Rprofile to suppress pre-loading the packages to be updated.

Selecting a Default CRAN Mirror from the R app

If the Package Installer does not show a package that you want, perhaps the mirror website you are using is not up-to-date. From the top menu bar for the R app, select RPreferences, Startup. Then under Default CRAN Mirror, click Select and scroll down in the popup window to one of the USA mirrors. "CA 1" is UC Berkeley, "CA 2" is UCLA, are usually up-to-date.  There is no "save" button on the preferences, they are apparently saved automatically.

Or you can use the chooseCRANmirror() function from R.

GeographicLib Geoid Models in MacOS

If you want to use the geoid_height function in eolsonde, you need one or more geoid models, which are available in tar.bz2 files from http://sourceforge.net/projects/geographiclib/files/geoids-distrib/.  After downloading, install them to /usr/local/share/GeographicLib. This example installs two models, egm96-5 and egm2008-1:

cd /usr/local/share
mkdir GeographicLib
cd GeographicLib
tar xjf ~/Downloads/egm96-5.tar.bz2
tar xjf ~/Downloads/egm2008-1.tar.bz2

Installing a Package from Source Form

If you need to install an R package from its source form, which contains C, C++ or Fortran code, then you'lll need to install Xcode and the Xcode command line tools from Apple. If the package requires extra open-source software, you will also need a software build package, such as HomeBrew, or MacPorts.

Dependencies of EOL Packages

The following packages are used by the EOL packages, so if you're building the EOL packages from source form, you will likely have to install these first. Enable  Install Dependencies so that their dependencies are installed also.

  • splusTimeDate
  • splusTimeSeries
  • quantreg
  • Sparse
  • Rcpp
  • RUnit
  • lattice
  • maps
  • gWidgets2
  • digest

Installing R in RedHat Linux

Note that on EOL servers, such as barolo, R and the local packages are installed and kept up to date, so R users on those systems shouldn't have to worry about any of the following.

Use yum or dnf to install R, RStudio and other software which is needed by add-on packages:

sudo yum install R rstudio netcdf netcdf-devel udunits udunits-devel
    hdf5-devel fftw fftw-devel gtk2-devel GeographicLib-devel

If one or more of the above packages are not available, you should configure your system to use the Extra Packages for Enterprise Linux.

Installing Contributed R Packages on RedHat Linux

Contributed packages can be installed from a command line R session, or from the Tools menu in RStudio.

Some packages may be automatically loaded via a library command in your .Rprofile or $(R RHOME)/etc/Rprofile.site startup files, or they may be required by another package which is automatically loaded. In order to successfully upgrade such a package, start a version of R that skips the .Rprofile and Rprofile.site initialization

R --vanilla

Then run install.packages() from that session, for example:

install.packages(c("digest","RNetCDF","splusTimeDate","splusTimeSeries",
    "quantreg","SparseM","Rcpp","RUnit","lattice","maps",
    "gWidgets2","gWidgets2tcltk"))

To install the EOL R packages:

install.packages(c("eolts","isfs","eolsonde"),
    repo="https://www.eol.ucar.edu/software/R")

To add the EOL repository to the list of searched repositories in R, do the following:

r <- getOption("repos")
r["EOL"] <- "https://www.eol.ucar.edu/software/R"
options(repos=r)

Installing packages from their source form is usually easy under Linux, if you have installed the development tools and the development packages shown above.

GeographicLib Geoid Models in RedHat Linux

If you want to use the geoid_height function in eolsonde, you need one or more geoid models. To install them on /usr/share/GeographicLib/geoid, use the geographic lib-get-geoids command, which is part of the GeographicLib RPM:

geographiclib-get-geoids -h
sudo geographiclib-get-geoids all

Windows

See R Project home page  and http://cran.r-project.org/doc/manuals/r-release/R-admin.html#Installing-R-under-Windows for information on installing R for Windows.

When updating packages, make sure there are no other instances of R running. Otherwise Windows will not allow files to be replaced in the R library directory.

When starting the R application by clicking on an icon from Windows Explorer or the desktop, the user's home directory for R is ?MyDocuments, which is where R will look for a .Rprofile, and an .RData. This is the value of the environment variable %USERPROFILE%.

When starting R from a command line shell, the user's home R directory is %HOMEPATH%, and R will look for .Rprofile and .RData there, if they are not found on the current directory.

Browsing the EOL R Repository

Due to security restrictions, a browser cannot list the contents of the local repository at  http://www.eol.ucar.edu/software/R, but you can view what packages are available, by entering the exact URL of a PACKAGES file:

​Rprofile.site on EOL Linux Systems

On tikal and barolo, a custom Rprofile.site file (/usr/lib64/R/etc/Rprofile.site) is automatically sourced when you run R or RStudio on those systems. This Rprofile.site does a source of the following two files if they exist:

  • $ISFS/scripts/Rprofile.isfs
  • $SONDE_ROOT/R/Rprofile

Amongst other things, these Rprofiles load the isfs and eolsonde packages. To suppress that behaviour, unset the ISFS and SONDE_ROOT environment variables in your shell before running R or RStudio, using the unset command in Bash or unsetenv in the C shell:

bash: unset ISFS SONDE_ROOT
tcsh: unsetenv ISFS SONDE_ROOT

 

Using RStudio at EOL

The URL is http://barolo.eol.ucar.edu:8787/