Installing and Using R

Installing R and Contributed Packages

 

Using R with ISFS data

Contributed Packages

There are many add-on R packages, contributed by the R community, 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/NCAR/R-eol, along with a wiki https://github.com/NCAR/R-eol/wiki.

An R package developed locally for analyzing and visualizing NCAR-Aircraft Data Files is:

The code for Ranadu is also on github, at https://github.com/NCAR/Ranadu.

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 repository here.

To view and install these packages with the Package Installer, select Other Repository, instead of CRAN (binary), and enter https://archive.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. 

Supported versions of R and macOS in EOL Binary Packages
R macOS Binary Packages 
4.2 12.3 Monterey yes
4.1 11.4 Big Sur, 12.3 Monterey yes
4.0 10.15 Catalina yes

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 RPreferencesStartup. 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://archive.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://archive.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://archive.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

Users .RProfile

Some useful statements in .Rprofile, which would typically go in your $HOME directory:

if (interactive()) {

    # Don't create .RData files everywhere

    # .RData on $HOME

    # setwd("~")

    # .RData on a scratch directory

    setwd(file.path("/scr/tmp",Sys.getenv("USER")))

    if (Sys.getenv("R_GUI_APP_VERSION") == "") utils::loadhistory()



    # interactive debugging

    options(error=utils::recover)

}

Proliferation of .RData Files

When you exit R with the q() function, you are asked: "Save workspace image?". If you answer 'y', .RData and .Rhistory files will be created in the current directory. This can result in many of these files scattered around our disks.

If instead you want to always read the same .RData when you start, and save it when you exit, you can add these commands to your $HOME/.Rprofile. This will set your current directory to '~' which is your $HOME directory, and .RData and .Rhistory will always be read and written there.

   setwd("~")

    if (interactive() && Sys.getenv("R_GUI_APP_VERSION") == "") utils::loadhistory()

Or you could use your EOL scratch directory:

   setwd(file.path("/scr/tmp",Sys.getenv("USER")))

   if (interactive() && Sys.getenv("R_GUI_APP_VERSION") == "") utils::loadhistory()

If you have an .RData in a certain directory that you wish to use, or want to read files in that directory without specifying a directory path, you can create a custom .Rprofile there which does not do a setwd​

Interactive Debugging

The utils::recover() function allows one to investigate the state of the execution stack when an error occurs. Set options(error=utils::recover) in your .Rprofile, to make this the default behaviour.

When an error occurs you will be shown a listing of the current function call stack. When you chose a stack level, the browser() function is invoked at that level. Then you can use the ls() function to display what variables are in scope at that level, type the name of a variable to see its contents, and execute most any expression with those variables. Do c to exit the browser and return to the stack listing. Do 0 to exit the recover() function.

Do ? recover and ? browser within R for more information.