Adding R programs to a personal library
- Create a library for a version of R in your home directory that makes packages available whenever you run that version of R.
- Create isolated environments for each of your R projects with packrat.
Personal Global R Directory
Installing R packages locally is fairly straightforward.
- Load the R environment module:
module load R/3.02 - Launch R:
R - Install the package, using generic package_name here
install.packages("package_name", repos="http://cran.r-project.org") - If you do not already have a personal library directory for the version of R you are using, you will see a prompt like this:
Warning in install.packages("package_name", repos = "http://cran.r-project.org") :
'lib = "/opt/R/3.0.2/lib64/R/library"' is not writable
Would you like to use a personal library instead? (y/n) - Select 'y'.
- Select 'y' again when prompted to create the directory.
- Your package should download and install into the newly created personal library directory.
Packrat Project Directory
Packrat is an R package which allows you to create an isolated and portable environment for each of your R projects. Packrat is available within R environment modules on the Argon HPC cluster beginning with version 3.3.2. For most newer versions of R in modules named like "r-#.#.#*" (starting with version 3.6.2) and provided in stack environment modules named like "YYYY.#*" (starting with 2019.1), the "r-packrat" module is separate and you'll need to load it manually.
You can create a new project and use packrat from the R prompt like so:
- Create a directory where you want you want to store the project.
cd
to the directory.- Start R.
- At the R prompt, initialize the packrat project with
packrat::init()
From this point on, you are working in a packrat project, and any packages you install will go into a library within this project. To restart an existing packrat project, simply cd to the desired directory (step 2) and start R there (step 3) and the project will load automatically.
Alternately, you you might find it useful to use packrat from your shell. For example:
- mkdir ~/someproject
- cd ~/someproject
Rscript -e 'packrat::init(enter=FALSE)'
- Rscript -e 'install.packages(c("brms","rstanarm"))'