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
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 |
Creating an environment
Check Conda is installed:
conda --versionCreate a new environment:
conda create --name my_project python=3.11Activate it:
conda activate my_projectrenv 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.
Installing packages
Install packages into your active environment:
conda install numpy pandas matplotlibOr using pip (for packages not available via conda):
pip install package-nameconda 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.
Exporting & sharing
Export your environment to a file:
conda env export > environment.ymlA collaborator can recreate it exactly:
conda env create -f environment.ymlFor a cleaner, more portable file — export only what you explicitly installed:
conda env export --from-history > environment.ymlSnapshot your current package state:
renv::snapshot()A collaborator restores it with:
renv::restore()environment.yml or renv.lock are now as important as your code for reproducibility.
In practice
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()
|