Use packrat R package to create a personal environment of R packages.
Say your R code is in directory /scratch/$USER/projects/project1
cd /scratch/$USER/projects/project1Packrat package is already installed for module r/intel/4.0.3. You need to install it yourself if you use other R module version
## Do this if packrat is not available (already installed for r/intel/4.0.3)# install.packages("packrat") ## By default this will install packrat package into a sub-directory within your home directory## init packrat in project's directorypackrat::init(".")You will see:
Packrat mode on. Using library in directory:- "/scratch/$USER/projects/project1/packrat/lib"Error in library("reshape2") : there is no package called ‘reshape2’
install.packages("reshape2")Test
Create a script which calls reshape2 library
Run it from project1 directory
Now create new directory project2
Run the same file - shall fail
Option 1
copy packrat/packrat.lock file from project1 to project2 directory
Run the same file - all the packages shall be installed on the fly
Option 2
copy whole packrat directory from project1 to project 2
Run the same file - will just run
Option 3
use bundle (see below)
Test R file
print("hello")packrat::restore()library(reshape2)names(airquality) <- tolower(names(airquality))head(airquality)aql <- melt(airquality)print("hello again")For testing run it as
srun --pty /bin/bashNote: your '.Rprofile' file will include line source("packrat/init.R")
If you copy the whole directory of your project and don't want to use packrat remove it
Keep only the packages that you use in this particular project (not all the packages available on the system)
R # launch Rpackrat::clean() # Remove unused packages from your library.Before you launch sbatch job, you need to make sure packrat environment is ready
You can restore as described below using packrat::restore() or packrat::unbundle()
When you launch a job with sbatch, R will check if there is packrat directory, and if packrat is on it will pick up packages, installed using packrat in the current directory
If you already have file packrat.lock or bundle file skip step 1
1. In the original location (your own laptop for example) go to project directory and execute
(Make sure the whole path to project directory and names of your script files don't have empty spaces!)
R# install.packages("packrat") ## if neededpackrat::init()packrat::on()packrat::clean()packrat::snapshot()## Or packrat::bundle() ## which will also save source code of packages2. Take file packrat/packrat.lock OR bundle file created above, and copy it to a new location for the project
3. At the new location - restore environment: go to directory of the project and execute. (Make sure version of R is the same)
## Create subdirectory "packrat"What is bundle
You can create a bundle file which will include all your project files, as well as a list of libraries with versions, plus the source files for libraries (no need to download anything later)
Packrat will install/compile what is needed on any system (Linux, Windows, etc)
You can share your code with other researchers no matter what system they use
Say you have a proejct1
go to the directory of project 1
start R
packrat::bundle()
You will see something like
The packrat project has been bundled at:
- "/scratch/$USER/projects/project1/packrat/bundles/project1-2019-11-08.tar.gz"
You can copy this file anywhere, then start R and unbundle it
packrat::unbundle("/scratch/$USER/projects/project1/packrat/bundles/project1-2019-11-08.tar.gz", where = "/scratch/$USER/projects/project3")
In order to have your work reproducible by you or/and others, save and/or commit your code in git, please including
packrat/packrat.lock (which lists all packages and versions that you use)
(optional) packrat/src/ (which will include all the source files for packages). This may be important if you use some custom packages, for which source code may be not available any more in the future (github repository deleted, etc.). Similar to bundle option described below
save version of R you are using