# packrat overview for R working group (Stephanie Labou, 02-22-2017) ###### What is packrat? ###### # Packrat solves a number of problems, including portability and reproducibility. # Packrat makes a "private package library" that is associated with a particular script (or project). # This means that you can work with different version of packages for different projects. # (This is very useful!!) # Additionally, you can send a "snapshot" (scripts plus private library) to somebody else, # who can then use exactly what you are using. # This is great for collborations! Especially in cases where everyone may have various package versions - # packrat let's you make sure that everyone is on the same page and your work is truly reproducible. # packrat is (potentially) VERY POWERFUL. We won't cover it all it all in this demo, # so I highly recommend you check out the main packrat site: packrat: https://rstudio.github.io/packrat/ ###### packrat demo ###### # First, you need to install packrat install.packages("packrat") # You'll also need to make sure your machine is able to build packages from source # For Windows, this means making sure you have Rtools: https://cran.rstudio.com/bin/windows/Rtools/ # See here for other operating systems: https://support.rstudio.com/hc/en-us/articles/200486498-Package-Development-Prerequisites # load packrat library library(packrat) # Set up a packrat project: # This will make your current working directory a packrat project. # I've named my folder "packrat_demo" and it will contains all the scripts, data, and other files for this pojrect # It will also contain the private packrat library associated with this project. packrat::init(".") # We are now in packrat mode, and everything we load, package-wise, will be placed in our private project library # Any packages you install from inside a packrat project are only available to that project # and packages you install outside of the project are not available to the project. library(ggplot2) #packrat is a blank slate, so I need to install packages I want from scratch install.packages("ggplot2") # Take a snapshot to save the changes in packrat packrat::snapshot() # Note packrat/packrat.lock # This lists the precise package versions that were used to satisfy dependencies, including dependencies of dependencies. # (This file should never be edited by hand!) # There is a lot more that you can do when building a packrat project from scratch. # See the walkthrough guide: https://rstudio.github.io/packrat/walkthrough.html # This is all well and good for keeping your own projects separate - no need to worry about package version conflicts! # What I think is most useful about package is sharing projects with "bundle" # Works like this: I bundle my project, I send it to a collaborator, then my collaborator unbundles it # and is working in *the same environment* I am. # No more worries about conflicting package versions from person to person or computer to computer. packrat::bundle() # I have written my project to a tar.gz file # So let's say I open a fresh R studio session with a fresh script in a separate folder: