Reproducibility should be the minimum standard for all computational social sciences.
Virtual environments promote reproducibility by letting you let you specify project-specific versions of packages.
“But it works on my computer!”
https://xkcd.com/1172
The renv package helps you create reproducible environments for your R projects. Use renv to make your R projects more isolated, portable and reproducible.
https://xkcd.com/2347
Me, after three hours without renv: Will, you’re an idiot–you’re using that one version of the Qualtrics API that’s broken.
Me, after .001 seconds with renv: Will, you’re an idiot–you need to install version 3.1.6 of Qualtrics: renv::install("qualtRics@3.1.6")
Open your .Rproj file, then your code
Use relative paths: here::here("data", "raw-data", "input1.csv")
Refer to R for Data Science for more on project workflows: https://r4ds.had.co.nz/workflow-projects.html
This adds to your project the infrastructure needed for renv.
Because organization is good and saves you time.
“… you can continue to use familiar tools like install.packages(). But you can also use renv::install(): it’s a little less typing and can install packages from GitHub, Bioconductor, and more, not just CRAN.” - The docs.
Straight from GitHub?
Yes. renv::install("UrbanInstitute/urbnthemes")
This records your packages (and their sources) in the lockfile, creating a reference for future you (and current and future collaborators).
renv.lock, to be exact. It’s JSON and you can open it in Notepad (or wherever).
git statusgit status
git add -A
git commit -m "a brief, descriptive message describing what you've done"
git push -u origin main
Cloning a repository? Pulling updates ? renv::restore() loads (and installs, if necessary) packages based on the lockfile.
Create GitHub repository.
Create your .Rproj file.
renv::init() to configure renv.
renv::install() to install packages.
renv::snapshot() to record your packages in the lockfile.
Push to GitHub.
renv::restore() to ensure your loaded packages reflect the lockfile.
Make sure your .gitignore includes:
renv.lock
.Rprofile
renv/settings.json
renv/activate.R
Happily, the renv/ folder by default contains its own .gitignore that will exclude unneeded files.
Sometimes not everything you want is reflected in your lockfile.
Ensure you’ve run renv::snapshot().
Try renv::install().
Run renv::dependencies() to check what packages renv is detecting.
Slack for help!
The workflow for renv should be the same on virtual desktops.
Process is the same as beginning-of-project setup.
Communicate with collaborators to ensure everyone’s aware of renv workflow.
If you’re feeling detail-oriented, set up renv, push to GitHub, then clone a fresh copy of the repository to check that everything’s working.