...
We
...
encourage
...
users
...
to
...
maintain
...
their
...
own
...
R
...
packages.
...
This
...
ensures
...
that
...
the
...
user
...
has
...
no
...
discontinuity
...
in
...
their
...
workflow
...
waiting
...
for
...
packages
...
to
...
be
...
installed
...
and
...
packages
...
work
...
with
...
the
...
user’s chosen,
...
loaded
...
R
...
module.
...
R
...
packages
...
are
...
built
...
specifically
...
to
...
a
...
version
...
of
...
R
...
and
...
may
...
not
...
work
...
properly
...
if
...
a
...
different
...
version
...
of
...
R
...
is
...
loaded.
...
O2’s LMOD “module” feature allows users to quickly and easily switch between R versions.
Table of Contents
Table of Contents | ||||
---|---|---|---|---|
|
General Commands
Command | Meaning |
---|---|
| shows the version of R installed on O2 |
| shows if other module(s) needs to be loaded to use R (e.g., |
| loads an individual module (i.e., |
| unloads an individual module (i.e., |
| shows the current version of R loaded |
| From R, shows |
Setting up a Personal R Library
This sets up an environment to store R packages locally. From R, all packages will be installed and saved to this R Personal Library by default. It is recommended to use a separate R library for each version of R selected (e.g., 3.6.1, 4.1.1, etc.). Multiple members of a group can share an R library saved in a group-accessible location by following the echo ...
and export ...
commands.
In O2 bash (hardcoded):
This approach is ideal for researchers using mainly a single R version and sharing the R Personal Library with other lab members.
Code Block | ||
---|---|---|
| ||
mkdir -p ~/R-VersionSelected<version_selected>/library mfk8@login02:~$ echo 'R_LIBS_USER="~/R-VersionSelected<version_selected>/library"' > $HOME/.Renviron mfk8@login02:~$ export export R_LIBS_USER="~/R-<version_selected>" |
In O2 bash (non-hardcoded alternative):
This alternative approach is ideal for researchers working concurrently with more than one R version or switching often among R versions. In this approach, the R Personal Library is automatically set based on the R module loaded.
Code Block |
---|
echo 'R_LIBS_USER="~/R/%p-VersionSelectedlibrary/library%V"' ``` ## Starting R Interactively R should not be executed from the login servers (login0X). Instead, launch an interactive session, specifying the memory requirements. R does not implicitly use multiple cores unless the code is parallelized, so 1 core is sufficient. Here's a model R workflow: ``` mfk8@login02> $HOME/.Renviron |
Info |
---|
Note: |
Starting R Interactively
R must be run in a compute node rather than a login node. To start an interactive session in a compute node, you need to specify the resources (i.e., walltime, cpus, and memory) via the $ srun
command and specify interactive
as the partition. For example:
Code Block |
---|
# 1. Start an interactive job (resources: 1 hour, 5GB of memory, and 1 cpu) mfk8@login01:~$ srun --pty -p interactive -t 0-121:00 --mem 8G5G -c 1 bash # 2. Load R/4.1.2 mfk8@compute-a-0116-01163:~$ module load gcc/69.2.0 R/34.1.2 # 3.5 #orStart versionR chosen mfk8@compute-a-0116-01163:~$ R > #notice how the prompt changes. ``` ## Installing Packages Using Bioconductor The majority of bio-related packages can be downloaded through Bioconductor, example installation given below. Please reference the [installation page on the Bioconductor website](https://www.bioconductor.org/install/) in case this method changes in the future. ```R > if (!requireNamespace. |
R does not implicitly use multiple core unless the code is parallelized. For researchers using the R package doParallel
, please check https://harvardmed.atlassian.net/wiki/spaces/O2/pages/1616511993/Job+Efficiency#R-package-doParallel
Installing Packages Using Bioconductor
The Bioconductor repository host a wide range of bio-related packages. To install Bioconductor, please check the installation page on the Bioconductor website in case the method described below changes in the future.
Code Block |
---|
if (!require("BiocManager", quietly = TRUE)) install.packages("BiocManager") > BiocManager::install(version = "3.1214") ``` User example: ```R > if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager") > BiocManager::install("affy") ``` Bioconductor packages are listed at <http://www.bioconductor.org/packages/release/BiocViews.html#___Software> ## Installing Packages Using CRAN The command **`install.packages()`** searches CRAN by default and asks you to select a mirror, but a repos can be specified using this command: ```R > install.packages("nameofpackage |
Panel | ||||||
---|---|---|---|---|---|---|
| ||||||
The latest Bioconductor release is 3.15 but requires >=R/4.2.0. Unfortunately, the patched version of R is not available yet to be installed as a module on O2. Nonetheless, HMS-RC will install R/4.2.1 once become available. |
Installing Packages Using CRAN
The command install.packages()
searches CRAN by default and ask you to select a mirror, but a repository can be specified using this command:
Code Block | ||
---|---|---|
| ||
install.packages("PkgName", repos="http://cran.r-project.org") ``` |
User
...
example:
Code Block | ||
---|---|---|
| ||
install.packages("foreignggplot2", repos="http://cran.r-project.org") > library(foreign) ``` A list of CRAN available packages is available at <httpggplot2) |
A list of CRAN available packages is available at http://cran.r-project.org/web/packages/
...
Installing
...
Packages
...
from
...
GitHub using Devtools
The R package devtools
allow researchers to install software from GitHub repositories. First, you need to install devtools
from the CRAN repo. Second, you need to load the R package using the library
command. Lastly, you use the install_github()
command to install R packages from GitHub. For example:
Code Block | ||
---|---|---|
| ||
# 1. Install devtools
install.packages("devtools")
# 2. Load R pkg
library(devtools)
# 3. Install a pkg from GitHub
# install_github("username/repo")
install_github("tidyverse/ggplot2") |
Installing Packages from a Local file
To install a package manually, place the compressed R package (usually is compressed as a tarball or zip file) inside your R Personal Library (e.g., ~/R-<version_selected>/library
) and run the following command:
Code Block | ||
---|---|---|
| ||
install.packages("pkgName", lib="~/R-VersionSelected<version_selected>/library") ``` Conversely, if all dependencies are already existing for the package, R CMD INSTALL can be used from the command line (on an interactive compute node): ``` mfk8@login02:~$ |
Conversely, you can install a package via R CMD INSTALL
. However, this assumes all dependencies are already installed in your R Personal Library. For example:
Code Block | ||
---|---|---|
| ||
# 1. O2 shell - start interactive job srun --pty -p interactive -t 0-121:00 --mem 80005G /bin/bash mfk8@compute-a-01-01:~$bash # 2. Load R modules module load gcc/69.2.0 R/34.1.2 # 3.5 #orInstall versionpkg selectedfrom mfk8@compute-a-01-01:~$a local file R CMD INSTALL downloadedRPackage.tar.gz ``` ## Installing Other R packages For installing other packages |
Installing Packages with Special Requirements
We’ve created a Confluence page with step-by-step instructions to install some commonly used R packages (e.g.,
...
SF, monocle3, etc.) requiring a few [non-common] extra steps - Installing Other R packages on O2. Often, the extra steps involve installing a system library. To simplify the process, we have installed those system libraries as modules on O2.
Loading R packages
Once the package is installed, the library(<pkg_name>)
can be used to load a package; for example:
Code Block | ||
---|---|---|
| ||
library("ggplot2") |