Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Markdown
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 that 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.

Related commands:

| Command                            | Meaning |
| ---------------------------------- |------------------ |
|  **`module spider R`**       |  shows the versions of R installed on O2 |
| **`module load gcc/6.2.0 R/version`**   |  loads an individual module |
|  **`module unload R/version`** |   unloads an individual module |
|  **`which R`**                      |   shows the current version of R loaded |
| **`> installed.packages()`**        |  from R, shows currently-installed packages |

## 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 library by default.  It is recommended to use a separate R library for each version of R selected (swap `Version Selected` for the R chosen, like 3.2.5 or 3.3.3, 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 (\$):

```
    mfk8@login02:~$ mkdir -p ~/R-VersionSelected/library
    mfk8@login02:~$ echo 'R_LIBS_USER="~/R-VersionSelected/library"' >  $HOME/.Renviron
    mfk8@login02:~$ export R_LIBS_USER="~/R-VersionSelected/library"
```
## 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:~$ srun --pty -p interactive -t 0-12:00 --mem 8G -c 1 bash
	mfk8@compute-a-01-01:~$ module load gcc/6.2.0 R/3.2.5 #or version chosen
	mfk8@compute-a-01-01:~$ 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("BiocManager", quietly = TRUE))
        install.packages("BiocManager")
    > BiocManager::install(version = "3.12")
```

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", repos="http://cran.r-project.org")
```

User example:

```E
    > install.packages("foreign", repos="http://cran.r-project.org")
    > library(foreign)
```

A list of CRAN available packages is available at <http://cran.r-project.org/web/packages/>

## Installing Packages from github using devtools


With the `devtools` package, users can install software from github repositories.  First install and call library on devtools, then use the `install_github()` command.


```
	> install.packages('devtools')
	> library(devtools)
	> install_github('username/repo')
```
## Installing Packages from Local File


If there is a package that needs to be manually downloaded (via wget/curl/etc), user should put the package in `~/R-VersionSelected/library` and install it like this:

In R (&gt;):

```R
    > install.packages("name-of-your-package", lib="~/R-VersionSelected/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:~$ srun --pty -p interactive -t 0-12:00 --mem 8000 /bin/bash
	mfk8@compute-a-01-01:~$ module load gcc/6.2.0 R/3.2.5 #or version selected
	mfk8@compute-a-01-01:~$ R CMD INSTALL downloadedRPackage.tar.gz
```



## Installing Other R packages


For installing other packages (e.g., sf or monocle3), please visit our [O2 wiki page](https://wikiharvardmed.rc.hms.harvard.edu/display/O2atlassian.net/wiki/spaces/O2/pages/1623425278/Installing+Other+R+packages+on+O2).


## Loading Packages

At the top of any script that uses an R package, the package must be explicitly called via **`library()`**.

```R
    > library("nameofpackage")
```

User case for a new R session, with these packages already installed:

```R
    > library(affy)
    > library(foreign)
```

...