R is a free software environment for statistical computing and graphics.
How to access R on CSCS machines
The R version provided on Cray systems comes with the Cray Programming Environment. As a consequence, the version is linked to the system software: if you need a newer version, go to the section “Installing a non-Cray version” below.
First you need to load the module for R tailored to our systems:
module load cray-R
To check the R version simply use:
module avail cray-R
Or, after loading the cray-R
module:
R --version
The version of R is also stored in the environment variable $CRAY_R_VERSION
, that is visible after loading the module cray-R
.
Then fire up the R session:
R
In the following sections, the actions to be done inside a R session will start with the R prompt >
.
Batch job with R using a Slurm submission script
The procedure is similar to using R interactively as shown before, but the command to be used is Rscript
. The following script shows an example of running a R script within a Slurm allocation using 2 nodes.
#!/bin/bash -l #SBATCH --job-name=R_batch #SBATCH --time=00:30:00 #SBATCH --nodes=2 #SBATCH --ntasks-per-core=1 #SBATCH --ntasks-per-node=1 #SBATCH --cpus-per-task=12 #SBATCH --constraint=gpu #SBATCH --account=<project> module load cray-R export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK Rscript script.r
Please replace the string <project>
with the ID of the active project that will be charged for the allocation.
How to install local R packages
This section shows you the steps to install R packages locally without root access on the machines.
A list of all the available R packages can be found here: https://stat.ethz.ch/CRAN/web/packages/index.html . Also, you can use the R specific search engine at http://www.rseek.org to find more packages.
R comes with a library folder which contains the standard and recommended packages. This is usually in a system location, that on Piz Daint is available after loading the modulecray-R
under $CRAY_R_PREFIX/lib64/R/library
.
R also has a default directory where users can install their own R packages that is ~/R/x86_64-suse-linux-gnu-library/3.4
. This directory doesn't exist by default. The first time a user installs an R package, R will ask if the user wants to use the default location: if the answer is yes, then it will create the directory.
The packages are compiled after download, usually with the GNU compiler. Therefore, please make sure that the GNU environment is loaded before installing packages:
module switch PrgEnv-cray PrgEnv-gnu
The R system and package-specific compilation flags can be overridden by setting the appropriate Make variables in the personal file $HOME/.R/Makevars
. For instance, the following file will call Cray compiler wrappers when building an R package that uses the variables $CC
, $CXX
, $FC
and $F77
:
CC=cc CXX=CC FC=ftn F77=ftn
The advantage of using Cray compiler wrappers is that include and library paths of modules loaded in the user's environment will be automatically adjusted. For more details, please refer to section 6.3.3 ("Customizing package compilation") of Chapter 6 ("Add-on packages") in the R installation guide.
A simple example
To install package optimization
, for example, use the package repository Swiss mirror located at ETH Zürich.
Load the package from inside R:
CC=cc CXX=CC FC=ftn F77=ftn
It gives a warning:
Warning in install.packages("optimization") : 'lib = "/opt/R/3.4.2/lib64/R/library"' is not writable Would you like to use a personal library instead? (y/n) y Would you like to create a personal library ~/R/x86_64-suse-linux-gnu-library/3.4 to install packages into? (y/n) y
Answering y
to both questions will create the user packages directory and install the package there.
If you need to install multiple packages, you can put them in the same install.packages
call using the array builder c()
. For example:
> install.packages(c("optimization", "devtools"), repos="https://stat.ethz.ch/CRAN/")
Setting the local R library path
If you want to use another location rather than the default one, for example, ~/local/R_libs/
, you need to create the directory first:
mkdir ~/local/R_libs
Then type the following command inside R
:
> install.packages("optimization", repos="https://stat.ethz.ch/CRAN/", lib="~/local/R_libs/")
It is a bit of burden having to type the long string of library path every time. To avoid doing that, you can create a file .Renviron
in your home directory, and add the following line to the file:
R_LIBS_USER=~/local/R_libs/
Whenever R is started, the directory ~/local/R_libs/
is added to the list of places to look for R packages and so:
> install.packages("optimization", repos="https://stat.ethz.ch/CRAN/")
will have the same effect as the previous install.packages()
command.
To see the directories where R searches for libraries, use the command:
> .libPaths()
Setting the repository
When you install a R package, you are asked which repository R should use to download the package from it. Generally, the Swiss mirror is the right choice, but you can choose another CRAN repository mirror using chooseCRANmirror()
inside R.
> chooseCRANmirror()
To set the repository and avoid having to specify this at every package install, create a file .Rprofile
in your home directory. This is the start up code for R. Add the following lines to the file:
cat(".Rprofile: Setting R repository:") repo = getOption("repos") # set up the server from which you will download the package. repo["CRAN"] = "https://stat.ethz.ch/CRAN/" options(repos = repo) rm(repo)
Now you only need to do:
> install.packages("optimization")
That will download the package optimization from https://stat.ethz.ch and install it in ~/local/R_libs
.
Updating packages
> update.packages()
inside an R session is the simplest way to ensure that all the packages in your local R library are up to date. It downloads the list of available packages and their current versions, compares it with those installed and offers to fetch and install any that have later versions on the repositories.
Note that attempting to update centrally installed packages will fail. You will have to use install.packages()
to install a newer copy in your personal directory instead. For example, if you are installing Bioconductor, the installation routine will try to update a bunch of centrally installed packages. To clone those into your personal library, tell biocLite()
not to update any of the packages, and copy the list from the output into your own install.packages()
command. See the example below (the c
function creates an array of strings in R).
> install.packages(c('Rmpi', 'assertthat', 'backports'))
Removing packages
> remove.packages("optimization") inside an R session to remove package optimization. An even easier way is just to go into the directory ~/local/R_libs and remove the directory optimization from there.
Installing packages from a script
The same effect of install.packages()
could be obtained from a script.
First you have to download the package(s) (as .tar.gz
files) with:
Rscript -e "download.packages(c('pbdMPI','rlecuyer'), '.', repos='http://stat.ethz.ch/CRAN/')"
Here as an example the packages pbdMPI
and rlecuyer
are downloaded in the current directory.
Then install the simplest one:
R CMD INSTALL rlecuyer*.tar.gz
The other one is more complex because it needs few options and arguments to be passed to the configure
script. This example package should be compiled with MPI:
R CMD INSTALL pbdMPI*.tar.gz --no-clean-on-error --no-test-load \ --configure-args="--disable-opa --with-mpi=$MPICH_DIR"
After a successful installation, the *.tar.gz
files can be deleted.
Installing packages from GitHub
Users can install R packages directly from Github using devtools
package as follows:
> library(devtools) > install_github("author/package")
Installing a non-Cray version
It is possible, using an EasyBuild recipe, to install locally a different version of R. EasyBuild documentation can be found on: https://user.cscs.ch/computing/compilation/easybuild/
The recipes for R are on GitHub: https://github.com/eth-cscs/production/tree/master/easybuild/easyconfigs/r/R
Download and eventually customize the one you are interested in. For example the latest R version available is R-3.6.2-CrayGNU-19.10.eb
Then run it this way:
module load EasyBuild-custom eb R-3.6.2-CrayGNU-19.10.eb -r
R will be installed under your home directory. For example for daint-gpu the installation directory is:
$HOME/easybuild/daint/haswell
To run this version of R:
module use easybuild/daint/haswell/modules/all module load R/3.6.2-CrayGNU-19.10 R
Further Documentation
R Home Page: www.r-project.org
Chapter 6 ("Add-on packages") in the R installation guide