Actions

Difference between revisions of "R on ALICE"

From ALICE Documentation

Line 23: Line 23:
 
   which R
 
   which R
 
   /cm/shared/easybuild/software/R/3.6.0-foss-2019a/bin/R
 
   /cm/shared/easybuild/software/R/3.6.0-foss-2019a/bin/R
 +
 +
===Running an R batch script on the command line===
 +
 +
There are several ways to launch an R script on the command line:
 +
 +
    Rscript yourfile.R
 +
    R CMD BATCH yourfile.R
 +
    R --no-save < yourfile.R
 +
    ./yourfile2.R
 +
 +
The first approach (i.e. using the Rscript command) redirects the output into stdout. The second approach (i.e. using the R CMD BATCH command) redirects its output into a file (in case yourfile.Rout).  A third approach is to redirect the input of the file yourfile.R to the R executable. Note that in the latter approach you must specify one of the following flags: --save, --no-save or --vanilla.
 +
 +
The R code can be launched as a Linux script (fourth approach) as well. In order to be run as a Linux script:
 +
 +
    One needs to insert an extra line (#!/usr/bin/env Rscript) at the top of the file yourfile.R
 +
    As a result we have a new file yourfile2.R
 +
    The permissions of the R script (i.e.yourfile2.R)need to be altered (-> executable)
 +
 +
The files seaice.R and seaice2.R can be used/seen as examples for yourfile.R, respectively yourfile2.R. Note that the scripts seaice.R and seaice2.Rrequire the data file sea-ice.txt.
 +
 +
Sometimes we need to feed arguments to the R script. This is especially useful if running parallel independent calculations - different arguments can be used to differentiate between the calculations, e.g. by feeding in different initial parameters. To read the arguments, one can use the commandArgs() function, e.g., if we have a script called myScript:
 +
 +
## myScript.R
 +
args <- commandArgs(trailingOnly =TRUE)
 +
rnorm(n=as.numeric(args[1]), mean=as.numeric(args[2]))
 +
 +
then we can call it with arguments as e.g.:
 +
 +
> Rscript myScript.R 5100[1]98.46435100.0462699.4493798.52910100.78853

Revision as of 09:25, 29 June 2020

Running R from batch scripts

R is a programming language and software environment for statistical computing and graphics.

The currently supported version is 3.6.0/3.6.2 (Centos7). 3.6.2 was built with the coda compilers. 3.6.0 was build using the standard GCC compiler.

load R in your environment?

You can obtain R in your environment by loading the R module i.e.:

 module load R/3.6.0-foss-2019a-Python-3.7.2

or

 module load R/3.6.2-fosscuda-2019b

The command R --version returns the version of R you have loaded:

 R --version
 R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
 Copyright (C) 2016 The R Foundation for Statistical Computing
 Platform: x86_64-pc-linux-gnu (64-bit)

The command which R returns the location where the R executable resides:

 which R
 /cm/shared/easybuild/software/R/3.6.0-foss-2019a/bin/R

Running an R batch script on the command line

There are several ways to launch an R script on the command line:

   Rscript yourfile.R
   R CMD BATCH yourfile.R
   R --no-save < yourfile.R
   ./yourfile2.R

The first approach (i.e. using the Rscript command) redirects the output into stdout. The second approach (i.e. using the R CMD BATCH command) redirects its output into a file (in case yourfile.Rout). A third approach is to redirect the input of the file yourfile.R to the R executable. Note that in the latter approach you must specify one of the following flags: --save, --no-save or --vanilla.

The R code can be launched as a Linux script (fourth approach) as well. In order to be run as a Linux script:

   One needs to insert an extra line (#!/usr/bin/env Rscript) at the top of the file yourfile.R 
   As a result we have a new file yourfile2.R
   The permissions of the R script (i.e.yourfile2.R)need to be altered (-> executable)

The files seaice.R and seaice2.R can be used/seen as examples for yourfile.R, respectively yourfile2.R. Note that the scripts seaice.R and seaice2.Rrequire the data file sea-ice.txt.

Sometimes we need to feed arguments to the R script. This is especially useful if running parallel independent calculations - different arguments can be used to differentiate between the calculations, e.g. by feeding in different initial parameters. To read the arguments, one can use the commandArgs() function, e.g., if we have a script called myScript:

    1. myScript.R

args <- commandArgs(trailingOnly =TRUE) rnorm(n=as.numeric(args[1]), mean=as.numeric(args[2]))

then we can call it with arguments as e.g.:

> Rscript myScript.R 5100[1]98.46435100.0462699.4493798.52910100.78853