Using R Packages
Creating a Custom R Library
R packages can be finicky. See Switching Between Custom Libraries and Common Problems below to help with frequent user issues.
Creating Your First Library
Make a local directory to store your packages:
$ mkdir -p ~/R/library
Tell R where the directory is by creating an environment file:
$ echo 'R_LIBS=~/R/library/' >> ~/.Renviron
That's it! Now you can install packages normally. For example, to install and load the package "ggplot2":
$ module load R $ R > install.packages("ggplot2") > library(ggplot2)
Switching Between Custom Libraries
If you're using different versions of R, we recommend you use different libraries. See Common Problems below for more information. When creating a library, consider including pertinent information in the name such as R version. For example:
If you start by using R version 4.0, following the instructions provided above:
$ mkdir -p ~/R/library_R_v4.0 $ echo 'R_LIBS=~/R/library_R_v4.0/' >> ~/.Renviron
If you later decide to switch to R version 4.1, instead of using your existing library, create a new one:
$ mkdir -p ~/R/library_R_v4.1
To use your new library, edit your .Renviron file:
$ nano ~/.Renviron # opens the R environment file for editing > R_LIBS=~/R/library_R_v4.1 # delete ~/R/library_R_v4.0 and enter the new directory > CONTROL + X # exits (remember to save at the prompt)
Now you can go about your business and install as you normally would.
Common Problems and How to Debug Them
Working on a cluster without root privileges can lead to complications. For general information on package installations, see the r-bloggers documentation. For information on common installation problems on our clusters, see the section below with with suggested solutions:
Using RStudio
How to Access RStudio
Setting a New User State Directory
When working on a large project in RStudio, it is possible for your R session's data to fill up your home directory resulting in out-of-space errors (e.g. when trying to edit files, create new OOD sessions, etc). With the newest version of RStudio, you can find these saved session files under ~/.local/share/rstudio
.
To preserve space in your home, you can specify a different directory by setting the environment variable RSTUDIO_DATA_HOME
. To do this, open the hidden file ~/.bashrc and add:
export RSTUDIO_DATA_HOME=/path/to/new/directory
where /path/to/new/directory
is the path to a different location where you have a larger space quota. For example, /groups/YOUR_PI/YOUR_NETID/rstudio_sessions
.
Setting Your Working Directory
Example R Scripts
We have examples for running R workflows in batch on our Github Examples page. Have a question or suggestion? Let us know!