R and RStudio
R is a language and environment for statistical computing and graphics. R provides a wide variety of statistical and graphical techniques, and is highly extensible.
One of R's strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formulas where needed. Great care has been taken over the defaults for the minor design choices in graphics, but the user retains full control.
RStudio is an integrated development environment for R. It includes a console, syntax-highlighting editor that supports direct code execution, as well as tools for plotting, history, debugging and workspace management.
RStudio is available in the OnDemand interface for Bridges. For more information, see the OnDemand documentation.
To see what versions of R are available type
module avail R
To see what other modules are needed, what commands are available and how to get additional help type
module help R
To use R, include a command like this in your batch script or type it in an interactive session to load the R module:
module load R
Be sure you also load any other modules needed, as given by the
module help R command.
Run the program with a command like
R --slave < input-file.R
Managing Local R Packages on Bridges
Bridges has optimized R installations that take advantage of Intel’s fast Math Kernel Library (MKL). It is important to use these optimized versions to get the best performance possible, but it can be challenging to manage the R packages you need for your work since you do not control the R installation. The instructions below provide recommendations on managing your own R packages while using optimized Bridges R installations.
Step 1: Choose the version of R you need
Decide which version of R on Bridges you’d like to use. R versions are labeled as major.minor.subminor (e.g. 3.4.1). You can see which versions are available by using the ‘module avail’ command:
$ module avail R ------------------------------ /opt/modulefiles --------------------------------- R/3.2.3-mkl R/3.3.1-mkl R/3.3.3-mkl R/3.4.1-mkl
If the version you need is not there, you can request that a new version be installed by emailing firstname.lastname@example.org.
Step 2: See what packages are already included
Many R packages are already installed with the versions of R provided on Bridges. To see what is included in a given version, you will need to load that version, start R, and use the
If you are looking for a particular package,
find.package will tell you if it is installed or not.
$ module load R/3.4.1-mkl $ R R version 3.4.1 (2017-06-30) -- "Single Candle" Copyright (C) 2017 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) [...] > find.package('splines')  "/opt/packages/R/3.4.1-mkl/lib64/R/library/splines" > find.package('junk') Error in find.package("junk") : there is no package called ‘junk’
To see all the installed R packages, use
installed.packages. This command lists a great deal of detail on the installed packages. The top of the output will show a list of the installed packages; you can parse the rest of the output for paths, versions, dependencies, and more.
$ module load R/3.4.1-mkl $ R R version 3.4.1 (2017-06-30) -- "Single Candle" Copyright (C) 2017 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) [...] > installed.packages() Package acepack "acepack" annotate "annotate" AnnotationDbi "AnnotationDbi" aod "aod" assertthat "assertthat" backports "backports" base "base" base64enc "base64enc" BH "BH" bindr "bindr" bindrcpp "bindrcpp" [...] XVector "XVector" yaml "yaml" zlibbioc "zlibbioc"
and much, much more.....
Step 3: Set up your R environment
If not all the packages you need are installed, or to have maximum control over your R environment, you can install R packages locally in your home directory. You will need to make a directory for your R packages and create a .Renviron file. Optionally, you can set a default CRAN repository to pull packages from.
- Create the directory where R should install your packages. Make sure the directory includes the major.minor version of R (e.g., 3.4) that you are using so you can have separate directories for each major.minor version of R. So, for example, if you want to use a version of R 3.4, you would create a directory like this:
$ mkdir -p Rpackages/3.4
- Create a .Renviron file in your home directory and set the R_LIBS variable to the full path of the directory you just created:
$ echo R_LIBS=`readlink -f Rpackages/3.4` > .Renviron $ cat .Renviron R_LIBS=/home/username/Rpackages/3.4
- (Optional) If you don’t want to manually install each R package, you can set up your environment to pull packages from a default CRAN repository. This will allow you to install R packages through an R script. To enable this, create a .Rprofile file in your home directory with the following lines:
r = getOption("repos") # hard code the US repo for CRAN r["CRAN"] = "http://cran.us.r-project.org" options(repos = r) rm(r)
Step 4: Install packages in R
Load R, run it interactively, and install the packages you need. You can also install packages via an R script if you completed step 3c.
If you ever change minor R versions (e.g., going from a version of 3.4 to 3.5), start over from step 1 and reinstall your packages. Technically you can reuse your previously installed packages in a new R minor version, but keeping them separate may help avoid compatibility issues.
- Load the R module
$ module load R/3.4.1-mkl
- Run R interactively
$ R R version 3.4.1 (2017-06-30) -- "Single Candle" Copyright (C) 2017 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) […]
- Install the packages you need
> install.packages("ggplot2") Installing package into ‘/home/username/Rpackages/3.4’ (as ‘lib’ is unspecified) trying URL 'http://cran.us.r-project.org/src/contrib/ggplot2_2.2.1.tar.gz' Content type 'application/x-gzip' length 2213308 bytes (2.1 MB) ================================================== downloaded 2.1 MB * installing *source* package ‘ggplot2’ ... ** package ‘ggplot2’ successfully unpacked and MD5 sums checked ** R ** data *** moving datasets to lazyload DB ** inst ** preparing package for lazy loading ** help *** installing help indices ** building package indices ** installing vignettes ** testing if installed package can be loaded * DONE (ggplot2) The downloaded source packages are in ‘/tmp/RtmpCj7BMg/downloaded_packages’