R

Z MetaCentrum
Skočit na navigaci Skočit na vyhledávání

Description

R is a language and environment for statistical computing and graphics. R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, ...) and graphical techniques, and is highly extensible. R is an integrated suite of software facilities for data manipulation, calculation and graphical display.

R includes

  • an effective data handling and storage facility,
  • a suite of operators for calculations on arrays, in particular matrices,
  • a large, coherent, integrated collection of intermediate tools for data analysis,
  • graphical facilities for data analysis and display either on-screen or on hardcopy and printed version.
  • a well-developed, simple and effective programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities.

See R View about and Parallel Computing with R.

Usage

Upcoming modulesystem change alert!

Due to large number of applications and their versions it is not practical to keep them explicitly listed at our wiki pages. Therefore an upgrade of modulefiles is underway. A feature of this upgrade will be the existence of default module for every application. This default choice does not need version number and it will load some (usually latest) version.

You can test the new version now by adding a line

source /cvmfs/software.metacentrum.cz/modulefiles/5.1.0/loadmodules

to your script before loading a module. Then, you can list all versions of R and load default version of R as

module avail R/ # list available modules
module load R   # load (default) module


If you wish to keep up to the current system, it is still possible. Simply list all modules by

module avail R

and choose explicit version you want to use. For batch regime, use your R file (commands.R) with commands for R

R CMD BATCH commands.R
  • Module r/r-4.1.1-intel-19.0.4-ilb46fy: R version 4.1.1
  • Module r/r-4.0.2-intel-19.0.4-5vzfhtq: R version 4.0.2
  • Module R-4.0.0-gcc: R version 4.0.0
  • Module R-4.0.0-intel-centos: R version 4.0.0
  • Module R-3.6.2-gcc: R version 3.6.2
  • Module R-3.5.1-gcc: R version 3.5.1 compiled for Debian 9
  • Module R-3.4.3-gcc: R version 3.4.3
  • Module R-3.4.0-gcc: R version 3.4.0
  • Module R-3.3.1-intel: R version 3.3.1 with blas, lapack and tcltk support
  • Module R-3.2.3-intel: R version 3.2.3 with blas, lapack and tcltk support
  • Module R-3.1.1: R version 3.1.1 with blas, lapack and tcltk support
  • Module R-3.1.0shlib
  • Module R-3.1.0: R version 3.1.0 with blas and lapack support
  • Module R-3.0.3
  • Module R-3.0.1: R version 3.0.1 with blas and lapack support
  • Module R-2.14.0: R version 2.14.0
  • Module R-2.4.0-kky: R version 2.4.0
  • Notice: This application use or needs GUI – graphical interface. To use the application in graphical mode see Remote desktop or X-Window.

R packages

Plenty of packages are already installed by MetaCentrum administrators, see /software/R/4.0.0/gcc/lib/R/library/ and analogical directories for different R versions. Other packages can be installed by user.

RStudio, OnDemand, Jupyter

R can be used in various ways. In batch job as R script or interactive work in R command line. Another option is usage of Rstudio --- full featured IDE, either as standard graphical application during interactive work, or in web browser using OnDemand. Support for R can be also added into Jupyter Notebook, or it can be directly used in Jupyter Lab.

Using Rmpi

How to use the Rmpi package? At first prepare a file hello_world_rmpi.R which will contain the following example:

# Load the R MPI package if it is not already loaded.
if (!is.loaded("mpi_initialize")) {
    library("Rmpi")
    }
# Spawn as many slaves as possible
mpi.spawn.Rslaves()
# In case R exits unexpectedly, have it automatically clean up
# resources taken up by Rmpi (slaves, memory, etc...)
.Last <- function(){
    if (is.loaded("mpi_initialize")){
        if (mpi.comm.size(1) > 0){
            print("Please use mpi.close.Rslaves() to close slaves.")
            mpi.close.Rslaves()
        }
        print("Please use mpi.quit() to quit R")
        .Call("mpi_finalize")
    }
}
# Tell all slaves to return a message identifying themselves
mpi.remote.exec(paste("I am",mpi.comm.rank(),"of",mpi.comm.size()))
# Tell all slaves to close down, and exit the program
mpi.close.Rslaves()
mpi.quit()

Add the openmpi modul and run the batch in "R"

module add R openmpi-1.6.5-intel
export OMPI_MCA_btl=sm,tcp,self
export OMPI_UNIVERSE_SIZE=`expr $TORQUE_RESC_TOTAL_PROCS - 1`
mpirun R CMD BATCH hello_world_rmpi.R

Notice: This application supports parallel computing (MPI, OpenMP) which can have weird consequences. For more details about parallel computing visit the page Parallelization.

UTF-8 support

This command sets the support for utf-8 at the beginning of the R script. For example where you need to import the Czech diacritics by function read.csv():

Sys.setlocale(category = "LC_ALL", locale = "en_US.UTF8")

Documentation

Documentation is available on CRAN or locally in program directory.

Homepage

URL: https://www.r-project.org/

FAQ (usefull links)

  • Everyone can easily create own R packages library, you just need some folder (ideally on the /storage tree), e.g. /storage/brno2/home/"$LOGNAME"/Rpackages/ ($LOGNAME is your login in MetaCentrum). Than you can install package by
install.packages("PACKAGE_NAME", lib="/storage/brno2/home/$LOGNAME/Rpackages/")

and load of such package

library(PACKAGE_NAME, lib.loc="/storage/brno2/home/$LOGNAME/Rpackages/")

To set the directory as default, you need to set R_LIBS variable before running R, e.g.

export R_LIBS="/storage/brno2/home/${LOGNAME}/Rpackages"

or edit .Rprofile[1], [2], [3].

To check installation path, you just run R a .libPaths() function and now you do not need to specify location of the directory with your own packages any more

R
.libPaths() # Outputs something like
[1] "/auto/brno2/home/$LOGNAME/Rpackages"
[2] "/afs/ics.muni.cz/software/R-3.1.0/lib/R/library"
install.packages("PACKAGENAME")
library("PACKAGENAME")
  • simple one core R script
#PBS -N helloR
#PBS -l walltime=2:00:00
#PBS -l select=1:ncpus=1:mem=4gb:scratch_local=10gb
# Clean scratch after the end
trap 'clean_scratch' TERM EXIT
# INPUT directory - with R script and other inputs
MYIN="/storage/brno2/home/${LOGNAME}/helloR"
# INPUT R script
MYR='hello.R'
# OUTPUT directory - where to store results
MYOUT="/storage/brno2/home/${LOGNAME}/helloRout"
mkdir -p "${MYOUT}" || exit 1
# Load R
module add R || exit 1
# Go to scratch directory
cd "${SCRATCHDIR}" || exit 1
# Copy all files from $MYIN
cp -a "${MYIN}"/* . || exit 1
# Run R
R -q --vanilla < "${MYR}" > "${MYR}".output
# Copy everything to MYOUT
cp -a * "${MYOUT}"/

script is send for computation by command

qsub helloR.sh