Using R Libraries
R Package Installation Best Practices
Many commonly used R packages are included in the base R installation available on HCC clusters,
such as tidyverse and stringr. However, users are able to install other packages in their
user libraries.
- Installing packages interactively
- Installing packages using R CMD INSTALL
- Creating an R Conda Environment
- Installing R Packages via OOD RStudio
Installing packages interactively¶
- Load the R module with the command
module load R- Note that each version of R uses its own user libraries. To
install packages under a specific version of R, specify which
version by using the module load command followed by the version
number. For example, to load R version 3.5, you would use the
command
module load R/3.5
- Note that each version of R uses its own user libraries. To
install packages under a specific version of R, specify which
version by using the module load command followed by the version
number. For example, to load R version 3.5, you would use the
command
- Run R interactively using the command
R - From within R, use the
install.packages()command to install desired packages. For example, to install the packageggplot2use the commandinstall.packages("ggplot2")
Some R packages, require external compilers or additional libraries. If you see an error when installing your package you might need to load additional modules to make these compilers or libraries available. For more information about this, refer to the package documentation.
Installing packages using R CMD INSTALL¶
To install packages using R CMD INSTALL the zipped package must
already be downloaded to the cluster. You can download package source
using wget. Then the R CMD INSTALL command can be used when
pointed to the full path of the source tar file. For example, to install
ggplot2 the following commands are used:
# Download the package source:
wget https://cran.r-project.org/src/contrib/ggplot2_3.2.1.tar.gz
# Install the package:
R CMD INSTALL ./ggplot2_2.2.1.tar.gz
Additional information on using the R CMD INSTALL command can be
found in the R documentation which can be seen by typing ?INSTALL
within the R console.
Creating an R Conda Environment¶
The R installation on HCC clusters includes many widely used packages, such as tidyverse and stringr. Users may install additional packages within their user space as required. For greater flexibility, a dedicated R environment can be created using Conda, allowing the installation of a specific R version along with any required packages.
To create the environment, first load the Anaconda module with the following command:
module load anaconda
Then, for example, to create an environment named myR with R 4.5 and some common libraries, run:
conda create -n myR R=4.5 r-essentials r-ggplot2 r-dplyr
After creation, activate the environment with:
conda activate myR
This sets up an isolated R environment including the selected libraries, giving you a flexible, self-contained workspace where you can manage packages and R versions independently for each project.`
Note: SLURM job examples using custom Conda environments can be found at https://github.com/unlhcc/job-examples/tree/master/conda.
Installing R Packages via OOD RStudio¶
Once you have your R environment set up, you can install additional R packages directly from OOD RStudio. Start RStudio through Open OnDemand. Then, you can install packages in the usual way using either CRAN or Bioconductor.
For example, to install ggplot2 package, use the following command:
install.packages("ggplot2")
Keep in mind, if Bioconductor packages need to be installed, then the Bioconductor R Variant should be used when launching RStudio.
Packages installed this way will be available only in the active R session or environment, keeping your work organized and reproducible.
Note: By default, installed packages are stored in your home directory, so they remain available across OOD sessions.
Note: The R packages installed using the RStudio Open OnDemand (OOD) App can only be accessible via the RStudio OOD App. To submit SLURM R job with custom R packages, the recommended way is to create custom R conda environment.