Skip to main content

R

R is a programming language designed for mathematical researchers, primarily used for statistical analysis, plotting, and data mining. R is an interpreted language (unlike C, which is compiled), so its execution speed is much slower than C, making it less suitable for optimization. However, it provides richer data structure operations at the syntax level and can easily output text and graphical information, making it widely used in mathematics, especially in statistics.


1. Script Template

submit.sh
#!/bin/bash
#SBATCH --nodes=1 # Number of machine nodes
#SBATCH --ntasks-per-node=56 # Number of cores per machine
#SBATCH --ntasks=56 # Total number of cores (total running cores)
#SBATCH --partition=g1_share # Queue partition (must specify the correct partition as instructed)
#SBATCH --job-name=hello # Job name (recommended to modify for distinction)
#SBATCH --output=hello.%j.out # Normal log output (%j parameter value is jobId)
#SBATCH --error=hello.%j.err # Error log output (%j is the jobId)
cd $HOME/test # Enter working directory
R --slave --no-restore --file=test.R # Change the script filename according to actual usage

2. Installing R on the Cluster

  1. Install R and environment via conda

    conda info --envs # View current conda environments
    conda create -n R3.5 # Create an environment named R3.5
    conda activate R3.5 # Activate the R3.5 environment
    conda install r-base=3.5.1 # Install R, specifying version 3.5.1
    conda deactivate # Exit the current environment
    conda remove --name R3.5 --all # Remove the R3.5 environment
  2. Upload local environment

    # Local environment must replicate cluster paths, otherwise R will fail to function
    # Incompatibilities exist between R versions; prefer versions consistent with previous environments

3. Methods for Installing R Packages

  1. Install R packages via conda

    conda install r-ps # Install R packages using the r-<package_name> format
  2. Offline installation of R packages

    # CRAN URL: https://cran.r-project.org/web/packages/available_packages_by_name.html
    # Enter R environment
    R
    install.packages('/es01/home/software/pack/ncdf4_1.19.tar.gz') # Specify the R package path inside parentheses; modify according to actual path
    # Do not enter R environment
    R CMD INSTALL foo_1.0.tar.gz
  3. Offline installation of R packages from Git

    # R packages from GitHub require extraction, e.g., unzip uataq-master.zip
    R
    library(devtools)# Load devtools
    devtools::install_local("/es01/home/software/pack/uataq-master")
  4. Common problem solutions

    # If installing local R packages requires dependency libraries or headers from the conda environment, add the required file paths to environment variables, reload variables, or re-login.
    source .bashrc
    # Edit the .Rprofile file in the home directory to configure paths for current or previously installed R packages, preventing the need to reinstall packages after re-downloading R. .Rprofile is not auto-generated and must be created manually.
    vim .Rprofile
    .libPaths(c("/es01/home/test/anaconda3/envs/r412/lib/R/library","/es01/home/test/R/lib64/R/library"))
    # First line is the path for conda-installed R packages; second line is the path for source-installed R packages

4. R Usage Commands

  1. Basic usage

    library() # View loaded packages
    library(devtool) # Load installed packages
    .libPaths() # View paths of installed R packages
    colnames(installed.packages()) # View installed R packages

Appendix:

  1. Conda installation in a networked environment

    # In a networked environment, install R using conda; recommended conda software and conda channels
    # conda software
    Anaconda3-5.2.0-Linux-x86_64.sh
    # Tsinghua conda mirror
    show_channel_urls: true
    remote_read_timeout_secs: 20000.0
    channels:
    - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
    - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64
    - defaults
    conda install r-base==4.1.2
  2. R source installation

    # R download link: https://cran.r-project.org/
    # curl download link: https://curl.se/download.html
    # Install curl
    ./configure --prefix=/es01/home/test/curl --with-amissl --with-bearssl --with-gnutls --with-mbedtls --with-nss --with-openssl --with-schannel --with-secure-transport --with-wolfssl --with-nss-deprecated
    make
    make install
    # Add curl to the environment variables and reload the environment variables
    export PATH=/es01/home/test/curl/bin:$PATH
    export LD_LIBRARY_PATH=/es01/home/test/curl/lib:$LD_LIBRARY_PATH
    export INCLUDE_PATH=/es01/home/test/curl/include/curl:$INCLUDE_PATH
    source .bashrc
    # Install R 3.6.3
    ./configure --prefix=/es01/home/test/R
    make
    make install

5. Error Handling

##1、Fatal error: cannot create 'R_TempDir'
## Investigation reveals this issue is caused by the root directory of the compute node being full. Simply clean up the Rtmp* files in the /tmp directory. Regular users can refer to the following operations:
srun -W 100000 -p g1_share -n 56 --pty /bin/bash
## Interactive job submission -p specifies the queue
df -h
## Check the status of the root directory
cd /tmp
rm Rtmp* -rf
# This process only handles cache files generated by the current account. If the cache was created by a different user, contact technical support.