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
-
Install R and environment via conda
conda info --envs # View current conda environmentsconda create -n R3.5 # Create an environment named R3.5conda activate R3.5 # Activate the R3.5 environmentconda install r-base=3.5.1 # Install R, specifying version 3.5.1conda deactivate # Exit the current environmentconda remove --name R3.5 --all # Remove the R3.5 environment -
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
-
Install R packages via conda
conda install r-ps # Install R packages using the r-<package_name> format -
Offline installation of R packages
# CRAN URL: https://cran.r-project.org/web/packages/available_packages_by_name.html# Enter R environmentRinstall.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 environmentR CMD INSTALL foo_1.0.tar.gz -
Offline installation of R packages from Git
# R packages from GitHub require extraction, e.g., unzip uataq-master.zipRlibrary(devtools)# Load devtoolsdevtools::install_local("/es01/home/software/pack/uataq-master") -
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
-
Basic usage
library() # View loaded packageslibrary(devtool) # Load installed packages.libPaths() # View paths of installed R packagescolnames(installed.packages()) # View installed R packages
Appendix:
-
Conda installation in a networked environment
# In a networked environment, install R using conda; recommended conda software and conda channels# conda softwareAnaconda3-5.2.0-Linux-x86_64.sh# Tsinghua conda mirrorshow_channel_urls: trueremote_read_timeout_secs: 20000.0channels:- 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- defaultsconda install r-base==4.1.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-deprecatedmakemake install# Add curl to the environment variables and reload the environment variablesexport PATH=/es01/home/test/curl/bin:$PATHexport LD_LIBRARY_PATH=/es01/home/test/curl/lib:$LD_LIBRARY_PATHexport INCLUDE_PATH=/es01/home/test/curl/include/curl:$INCLUDE_PATHsource .bashrc# Install R 3.6.3./configure --prefix=/es01/home/test/Rmakemake 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.