How to load Rockfish R submodules into an R session or R-Studio environment

This tutorial will guide you through the process of loading R submodules in an R session. These procedures also can be easily reproduced in various environments, such as R-Studio, R Script, or Slurm script.

In general, the module load command is used to load a specific software package or application into the current shell session. This command modifies the system’s environment variables, such as PATH or LD_LIBRARY_PATHto make the software package available to the user.
Note
R is an open-source programming language and software environment that is commonly used for statistical computing, data analysis, and visualization. By loading version 4.0.2 of R into the shell session, the user can run R scripts and commands, use R packages, and access other R-related functionality from within the terminal.

For instance, in this specific case, the module load command is being used to load version 4.0.2 of the R programming language into the current shell session on Rockfish.

Here is an example of how to load a submodule for R/4.0.2:

1. First, you would need to log in to a system where R/4.0.2 is installed and load the R module.

				
					[userid@local ~]$ module load r/4.0.2
				
			

2. Next, you would start an R session by typing R at the command line. This will open the R command line interface.

3. Once you are in the R command line interface, you can use the library() function to load the desired submodule. For example, if you wanted to load the ggplot2 package, which is a popular package for data visualization in R, you would type the following command:

				
					> library(ggplot2)
				
			
This command loads the ggplot2 package into the R session, making its functions and data available for use.

4. After you have finished using the submodule, you can unload it from the R session using the detach() function, to remove the ggplot2 package from the R session, freeing up memory and preventing conflicts with other packages.

				
					> detach("package:ggplot2", unload=TRUE)
				
			

Overall, loading submodules in R/4.0.2 is a matter of using the library() function to load R packages within the R command line interface. The specific packages and submodules you load will depend on your specific needs and goals.

However, if the ggplot2 package is not installed or not available in the system, you need install it using the install.packages() command..

				
					> install.packages("ggplot2")
				
			

This procedures will store the package/library in the user’s home directory (R_LIBS_USER), and it will be available for use in the R session

Another option is to source the lmod.R script. This provides additional functionality for managing Rockfish R submodules and loading them into the R session, which will be explained in the next section.

How to load R submodules available in the system in R session

The lmod.Rscript helps to load Rockfish R submodules available in the system into the R session.
Note
This script is available in the /data/apps/helpers/ directory on Rockfish. It will change the R_LIBS_USER variable in R returning the paths where R looks for installed packages, the same way module load do in the terminal. When R searches for a package that has been loaded or installed, it will search in each of the directories listed by .libPaths() until it finds the package it is looking for.
Here is an example of how to use the lmod.R script to load a submodule for R/4.0.2:
  1. First, you would need to log in to a system where R/4.0.2 is installed and load the R module using the module load command.

				
					[userid@local ~]$ module load r/4.0.2
				
			
  1. Next, you would start an R session by typing R at the command line. This will open the R command line interface.
  2. Once you are in the R command line interface, you can use the source() function to load the lmod.R script. For example:
				
					> source("/data/apps/helpers/lmod.R")
				
			

You can also use the source() function to load the lmod.R script from a different directory. For example:

source(file.path(Sys.getenv(“R_LIBS_USER”), “lmod.R”))

The file.path function in base R offers a convenient way to define a file path, Sys.getenv(“R_LIBS_USER”) returns the path store into R_LIBS_USER variable, and R_LIBS_USER is an environment variable that defines the location of the user’s personal R library directory.​

4. After you have sourced the lmod.R script, you can use the lmod() function to load the desired submodule. For example, if you wanted to load the ggplot2 package, which is a popular package for data visualization in R, you would type the following command: 

				
					> module("load", "r/4.0.2")
> module("load", "r-ggplot2")
				
			

The first load command will load the R module making R submodules available to the next command, and the second load command will load the ggplot2 package into the R session, making its functions and data available for use.

  1. After you have finished using the submodule, you can unload it from the R session using the lmod() function. For example:

				
					> module("unload", "r-ggplot2")
				
			
This command removes the ggplot2 package from the R session, freeing up memory and preventing conflicts with other packages.

Overall, loading submodules in R/4.0.2 is a matter of using the lmod.R function to load R packages within the R command line interface. The specific packages and submodules you load will depend on your specific needs and goals.

However, if the ggplot2 package is not installed, you need to install it using the install.packages() command. For example:

				
					> install.packages("ggplot2")
				
			

This command will install the ggplot2 package into the R session, making its functions and data available for use.

How to load tidyverse R submodule in R session

				
					[userid@local ~]$ module load r/4.0.2
[userid@local ~]$ R

> module("load", "r-tidyverse")
> library(tidyverse)

				
			

How to load R submodules and install Rsamtools package in R session

				
					[userid@local ~]$ module load r/4.0.2
[userid@local ~]$ R
				
			
Warning
The lmod.R only works with r/3.6.3 or r/4.0.2.
				
					> source(file.path(Sys.getenv("R_LIBS_USER"), "lmod.R"))
> module("load", "r/4.0.2")
				
			
Tip
Loading the r/4.0.2 will make R submodules available in the R session. Note: It won’t work if you use a different R version loaded in the terminal. Change the version as needed.
				
					> module("load","libjpeg")
> module("load","libpng")
> module("load","bzip2")
> module("load","curl")

> if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
> BiocManager::install("Rsamtools",dependencies=TRUE, force=TRUE)

> library(Rsamtools)
				
			

How to load and and list submodule in R session

In this example we will load the ggplot2 submodule and list all the submodules loaded in the R session, using another R version.

				
					[userid@local ~]$ module load r/4.0.2
[userid@local ~]$ R

> module("load","r-ggplot2/3.2.0")
> module("list")

Currently Loaded Modules:
  1) gcc/9.3.0         10) r-lazyeval/0.2.2   19) r-magrittr/1.5      28) r-rcolorbrewer/1.1-2  37) r-ellipsis/0.3.0
  2) openmpi/3.1.6     11) r-mass/7.3-51.5    20) r-stringi/1.4.3     29) r-viridislite/0.3.0   38) r-zeallot/0.1.0
  3) slurm/19.05.7     12) r-lattice/0.20-38  21) r-stringr/1.4.0     30) r-scales/1.0.0        39) r-vctrs/0.2.0
  4) helpers/0.1.1     13) r-matrix/1.2-17    22) r-reshape2/1.4.3    31) r-assertthat/0.2.1    40) r-pillar/1.4.2
  5) git/2.28.0        14) r-nlme/3.1-141     23) r-rlang/0.4.6       32) r-crayon/1.3.4        41) r-pkgconfig/2.0.2
  6) standard/2020.10  15) r-mgcv/1.8-28      24) r-labeling/0.3      33) r-fansi/0.4.0         42) r-tibble/2.1.3
  7) r/3.6.3           16) r-rcpp/1.0.4.6     25) r-colorspace/1.4-1  34) r-cli/2.0.2           43) r-withr/2.2.0
  8) r-digest/0.6.25   17) r-plyr/1.8.4       26) r-munsell/0.5.0     35) r-utf8/1.1.4          44) r-ggplot2/3.2.0
  9) r-gtable/0.3.0    18) r-glue/1.4.1       27) r-r6/2.4.0          36) r-backports/1.1.4
>
				
			

Also, you can use the module() function to list all of the available modules in the current Lmod system. For example:

> module(“avail”)

> module(“spider”,”r-“)

> module(“list”)

This command lists all of the available modules in the current Lmod system. Running this command can be useful if you are not sure which module you need to load for a particular task.