Environments

Context for your code

Think of your coding workspace like a house.

Just as a blender belongs in the kitchen and not the bedroom — packages are installed per environment, not globally. And just like pots and pans need a stove, some packages depend on others to function.

An environment captures the exact software context your code needs to run:

  • A specific version of Python or R
  • A specific set of packages, each pinned to a specific version
  • The dependencies those packages require
NoteThe environment file is like a blueprint: one file for each room, which documents exactly what is in it, the colour, size, make, model, and precise position. Anyone with the blueprint can recreate the room exactly.

Tools

There are a host of tools to manage environments — the right choice depends on your language and workflow.

Tool Language Best for
Conda Python (+ R) Data science projects with many dependencies
venv / virtualenv Python Lightweight Python-only environments
renv R R package dependency management
Docker Any Complete reproducibility & isolation
NoteWe will focus on Conda (Python) and renv (R) — usually the most practical choices for research workflows.

Creating an environment

Check Conda is installed:

conda --version

Create a new environment:

conda create --name my_project python=3.11

Activate it:

conda activate my_project

renv works inside an RStudio project — no separate install needed.

Open or create an RStudio project, then initialise renv:

install.packages("renv")
renv::init()

This creates a project-local library and a renv.lock file to track your packages.

ImportantAlways activate your environment before installing packages or running code — otherwise changes go into your base environment.

Installing packages

Install packages into your active environment:

conda install numpy pandas matplotlib

Or using pip (for packages not available via conda):

pip install package-name
NotePrefer conda install where possible — it handles dependency conflicts better than pip. Use pip only for packages unavailable on conda.

Install packages as normal — renv tracks them automatically:

install.packages("ggplot2")
install.packages("tidyverse")

renv records each install in renv.lock.

NoteAny package installed while renv is active is automatically tracked. No extra steps needed!

Exporting & sharing

Export your environment to a file:

conda env export > environment.yml

A collaborator can recreate it exactly:

conda env create -f environment.yml

For a cleaner, more portable file — export only what you explicitly installed:

conda env export --from-history > environment.yml

Snapshot your current package state:

renv::snapshot()

A collaborator restores it with:

renv::restore()
Tipenvironment.yml or renv.lock are now as important as your code for reproducibility.

In practice

CautionExercises

1.5 Open the analysis folder and locate the script for your preferred language (Python or R)

1.6 Create a new environment for this project

1.7 Install the packages required for the example script

1.8 Export the environment file

Quick reference

🐍 Python (Conda) R (renv)
conda create –name proj python=3.11
Create environment
renv::init()
conda activate proj
Activate environment
active within project
conda install numpy pandas …
Install packages
install.packages(“pkg”)
write your scripts
Do your work
write your scripts
conda env export > environment.yml
Export environment file
renv::snapshot()