1-data/
├── raw/
│ └── fixtures.csv
└── clean/ ← create this folder; it starts empty
2-scripts/
├── 01-clean.R
├── 02-analysis.R
└── 03-plot.R
outputs/ ← create this folder; it starts emptyReproducible workflows in R
Practical
Welcome
This practical will bring together the steps covered in the lecture by:
- Building a reproducible R pipeline
- Committing your project to GitHub
- Restoring it from GitHub
In June 2026, Spain beat England 4-0 — England’s heaviest defeat in 17 years — and denied them automatic qualification for the 2027 World Cup. The two sides meet again in the UEFA Women’s Nations League on 15 November 2026. We’ll build a reproducible pipeline to predict England’s chances in that rematch, using their real results over the past year.
You will:
- Build models to predict England’s goals scored and conceded.
- Simulate the match to estimate the chance of a win, draw or loss.
- Use
herefor file paths andrenvto lock package versions. - Create a Git repository, version your code with git, and push it to GitHub.
- Make the project fully reproducible and shareable.
Setup
Software
To complete this practical, you’ll need:
- R (≥ 4.5)
- RStudio
- Git, used for version control, plus either:
- GitHub Desktop, a user-friendly way to interact with Git and GitHub; or
- the GitHub CLI (
gh), if you’d rather work at the command line.
GitHub Desktop includes its own copy of Git, so if you only ever use GitHub Desktop, that’s all you need.
Installing Git
Git is often pre-installed on macOS. You can check by typing in the terminal:
git --versionIf it’s not installed, run:
xcode-select --installYou can optionally install GitHub Desktop, a graphical interface for Git and GitHub, by following the instructions here.
You can download Git for Windows from https://git-scm.com. Run the installer and accept default options, in particular:
“Use Git from the command line and also from 3rd-party software”
You can optionally install GitHub Desktop, a graphical interface for Git and GitHub, by following the instructions here.
Telling Git who you are
Git records a name and email address against every commit. If you have never used Git on this machine, set these once:
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
git config --global init.defaultBranch mainUse the same email address as your GitHub account, so that your commits are linked to your profile. If you’re using GitHub Desktop, the name and email are filled in for you when you sign in.
Authenticating with GitHub
To push and pull from GitHub, you need to authenticate. Pick whichever route you’ll be using for the rest of the practical:
GitHub CLI (gh) is GitHub’s official command line tool. It handles authentication for you, and later saves us a trip to the browser when creating the repository.
Install it:
- macOS:
brew install gh(or download from cli.github.com) - Windows: download the installer from cli.github.com
- macOS:
Log in:
gh auth loginFollow the prompts: choose GitHub.com, then HTTPS, then Login with a web browser. Copy the one-time code shown in the terminal, press Enter, and paste it into the page that opens.
Check it worked:
gh auth statusYou should see
Logged in to github.com as your-username.
GitHub Desktop is a good place to start if you’re new to Git.
When you first sign in via the application, it stores a token securely on your computer. You can clone, commit, push, and pull without dealing with passwords or keys.
Using SSH keys is recommended for longer-term use.
To set them up will require access to a command line.
Generate a key (if you don’t have one):
ssh-keygen -t ed25519 -C "your_email@example.com"Copy the key to your clipboard
pbcopy < ~/.ssh/id_ed25519.pubcat ~/.ssh/id_ed25519.pub | clip- Add it to GitHub:
- Go to GitHub → Settings → SSH and GPG keys.
- Click New SSH key and paste your generated key.
See these instructions if you get stuck.
If you use SSH, use the git@github.com:... form of the repository URL wherever https://github.com/... appears below.
Pre-flight check
Before starting, check that everything is in place. It’s much quicker to fix this now than halfway through.
- 1
-
You should see a version number, e.g.
git version 2.50.1. - 2
- You should see the name you set above.
- 3
-
You should see
Logged in to github.com. If you getgh: command not found, install the GitHub CLI as described above; if you’re not logged in, rungh auth login.
- Open GitHub Desktop.
- Check you’re signed in: GitHub Desktop → Settings → Accounts (macOS), or File → Options → Accounts (Windows). You should see your GitHub username.
- If not, click Sign in to GitHub.com and follow the prompts.
1 Create a project in RStudio
Create a new RStudio Project and give it an appropriate name (e.g., england-prediction):
File → New Project → New Directory
2 Download the scripts and datasets
Right click the links below to download the required scripts and dataset:
The fixtures.csv file contains England’s real results over the past year. If you’re curious how it was assembled, the optional 00-fetch.R script documents where the data came from — but you don’t need to run it.
Put these inside your project folder and recreate the structure shown below:
The 1-data/clean/ and outputs/ folders are empty to begin with — the scripts will fill them. You can create them by hand, or from the R console:
dir.create("1-data/clean", recursive = TRUE)
dir.create("outputs")3 Initialise renv and install the required packages
In the R console, type:
- 1
-
Install the
renvpackage. You only need to do this once—not for each project. - 2
-
Initialise
renvfor the current project.
renv::init() does three things: it scans your scripts for library() calls, installs the packages it finds, and writes them to a lockfile called renv.lock. Because the scripts are already in the project folder, this is all you need — there’s no separate install step. This may take a few minutes the first time.
It also creates a hidden .Rprofile file at the project root containing a single line, source("renv/activate.R"). That line is what switches renv on each time the project is opened, so it matters later when we commit our files.
Open the renv.lock lockfile to understand its contents.
4 Run the three scripts
Once packages are installed and renv is initialised, you’re ready to run the analysis.
Run 01-clean.R in RStudio. This script:
- Imports the
fixtures.csvdataset from the1-data/rawfolder. - Derives new variables: whether each match was won, drawn or lost; days of rest since the previous fixture; and recent form (wins and goals scored over the previous three matches).
- Saves a cleaned dataset to
1-data/clean/fixtures.rds.
Run 02-analysis.R in RStudio. This script:
- Fits two Poisson regression models to predict:
- England’s goals
- Spain’s goals
- Simulates 10,000 match results
- Saves the probabilities to
outputs/results.rds
Run 03-plot.R in RStudio. This script:
- Loads the saved probabilities (
outputs/results.rds). - Creates a bar chart of predicted win/draw/loss.
- Saves the figure to
outputs/prediction_plot.png.
5 Putting it all together
Create a new script run.R at the project root with the contents:
run.R
library(here)
source(here("2-scripts", "01-clean.R"))
source(here("2-scripts", "02-analysis.R"))
source(here("2-scripts", "03-plot.R"))This script uses source to run the three scripts sequentially, avoiding the need to run them separately.
Run the run.R script either by clicking Run or by typing at the console:
source("run.R")6 Initialise the Git repository
We’ve now set up our project, initialised renv, and created a run.R script that automates our data cleaning and analysis.
In this section, we’ll initialise a new, empty Git repository. Git will allow us to track changes to our files over time and restore previous versions.
You can complete this section via the terminal or using a desktop application, such as GitHub Desktop.
Open a terminal in the project root and type:
git init- You can do this from within RStudio in the ‘Terminal’ pane.
- If you haven’t yet installed Git, see the instructions above.
- Open GitHub Desktop.
- Go to File → Add Local Repository….
- Click Choose… and select your existing project folder.
- You should see the prompt:
The directory does not appear to be a Git repository. Would you like to create a repository here instead?”
- Click “Create Repository”.
7 Tell Git what to ignore
Not everything in a project folder belongs in version control. Some files are generated automatically, some are specific to your computer, and some (e.g., data) must never leave your machine at all.
Create a file called .gitignore at the project root containing:
.gitignore
.Rproj.user/
.Rhistory
.RData
.Ruserdata
.DS_Store
*.logA ready-made version is available here: .gitignore.
Each line is a pattern; *.log ignores every file ending in .log, wherever it appears. In GitHub Desktop you can also right-click a file or folder in the Changes panel and choose Ignore file or Ignore folder, which writes these lines for you.
8 Commit files to the local repository
Having initialised the empty repository, we now need to add our files.
- 1
- Add the data, scripts and outputs folders.
- 2
-
Add the
run.Rscript, therenv.locklockfile and your new.gitignore. - 3
-
Add the renv machinery. Don’t skip this — without
.Rprofileandrenv/activate.R, renv won’t switch itself on when someone else opens the project, and the restore step later won’t work. - 4
- Add your RStudio Project file; change the name as appropriate.
- 5
- Check what is about to be committed before committing it.
- 6
-
Commit the staged files with a short message (specified by
-m)
Open GitHub Desktop and select your repository.
In the Changes tab, tick the checkboxes to stage the following:
- All files in the
1-data/,2-scripts/andoutputs/folders - The
run.Rfile - The
renv.lockfile and your new.gitignore .Rprofile,renv/activate.Randrenv/settings.json— these are hidden files, so make sure hidden files are visible (see above). Without them, renv won’t switch itself on for anyone who clones your project.- Your
.Rprojfile
- All files in the
At the bottom left, enter a commit message:
Initial commitClick Commit to main.
Looking at your history
You’ve made one commit. Take a moment to look at it:
git log --onelineClick the History tab at the top of the left panel. You should see your commit, with its message and timestamp.
9 Send your repository to GitHub
Your repository currently exists only on your computer. Now we’ll put a copy on GitHub. If you haven’t already, create a free account at GitHub.com — using your KCL email address is a good idea for research repositories.
The GitHub CLI can create the repository on GitHub, connect it to your local one, and push — all in a single command. From your project folder:
gh repo create england-prediction --private --source=. --push--privatekeeps the repository private; you can always make it public later. Use--publicif you’d rather share it now.--source=.uses the repository in the current folder.--pushpushes your commits straight away.
GitHub Desktop can create the repository on GitHub for you — you don’t need to visit the website first.
In GitHub Desktop, click Publish repository in the top bar.
Check the name (e.g.,
england-prediction) and tick Keep this code private. You can always make it public later.Click Publish repository.
Now go to https://github.com/username/england-prediction in your browser (gh repo view --web will open it for you). You should see your files and your commit.
10 The everyday loop
Almost everything you do with Git from now on is the same three steps: make a change, commit it, push it. Let’s practise once.
Step 1. Make a small edit — add a comment to one of the scripts, or create a new file called NOTES.md.
Step 2. Commit and push:
- 1
- See which files have changed.
- 2
-
Stage all of the changes. (
.gitignorekeeps the unwanted ones out.) - 3
- Commit them with a message describing what you changed, not just “changes”.
- 4
- Send them to GitHub.
- Your change appears in the Changes panel.
- Write a clear commit message and click Commit to main.
- Click Push origin in the top bar.
Step 3. Refresh your repository page on GitHub. Your new commit should be at the top of the history.
11 Reproducing your analysis from GitHub
We’ll now test that we can recreate our analysis from the online repository. This ensures your project can be reliably re-run on another computer or by another user.
The steps involved are:
- ‘Clone’ the existing repository from GitHub.
- Restore the
renvenvironment. - Run the
run.Rscript to repeat the analysis.
In the terminal:
- 1
- Move somewhere other than your existing project folder.
- 2
-
‘Clone’ the repository from GitHub into a new folder called
test-clone. Replaceusernameand the repository name as appropriate.
Then open the .Rproj file in the cloned folder, and at the R console:
- 1
-
Download and install the exact package versions recorded in
renv.lock. This can take a few minutes. - 2
- Re-run the whole pipeline.
- File → Clone Repository…
- Choose your repository.
- Under Local path, choose a location outside your existing project folder (e.g.,
Desktop/test-clone).
Then open the .Rproj file in the cloned folder, and at the R console:
- 1
-
Download and install the exact package versions recorded in
renv.lock. This can take a few minutes. - 2
- Re-run the whole pipeline.
When you open the cloned project, you should see a message from renv in the console, something like “Project ‘~/Desktop/test-clone’ loaded. [renv 1.1.x]”. That message comes from .Rprofile — if you don’t see it, .Rprofile or renv/activate.R probably didn’t make it into your commit.
Check that outputs/prediction_plot.png has been regenerated inside the cloned folder. If it has, your project is genuinely reproducible: everything needed to recreate the analysis travelled with the repository.
12 Adding a ‘README’ file
- Write a
README.md1 file in your project folder. Briefly describe the analysis and steps needed to reproduce. - Add and commit this file to your local repository, and push the changes to GitHub — the same everyday loop as before.
Once pushed, you can view the README.md on your repository page on GitHub.
Optional extensions
You’ve now built, versioned, and shared a fully reproducible pipeline — that’s the heart of today’s session. If you have time, pick one of the extensions below to take it further. They’re independent, so you don’t need both.
Goal: turn your pipeline’s output into a reproducible report that rebuilds its tables and figures every time it’s rendered.
Create a new file
report.qmdat your project root (next torun.R). A ready-made template is available here:report.qmd.It reads the saved results and displays the prediction:
--- title: "Can England beat Spain?" format: html execute: echo: false --- ```{r} library(tidyverse); library(here) results <- readRDS(here("outputs", "results.rds")) knitr::kable(results) ``` Render it with the Render button in RStudio. (If you’d rather render from the console, install the R package first with
renv::install("quarto"), then runquarto::quarto_render("report.qmd").)Commit and push
report.qmd. Its rendered output,report.html, is generated from the source, so add it to.gitignorerather than committing it.
Goal: run the whole pipeline inside a container so it behaves identically on any machine, regardless of what’s installed.
We build on a pre-built Rocker image (which already contains R and the tidyverse), so you’re not installing everything from scratch. From your project folder:
Create a
Dockerfileat the project root — a ready-made one is here:Dockerfile. It restores your exact packages fromrenv.lock:FROM rocker/tidyverse:4.5.1 WORKDIR /project COPY renv.lock renv.lock RUN R -e "install.packages('renv'); renv::restore(prompt = FALSE)" COPY . . CMD ["Rscript", "run.R"]Add a
.dockerignorealongside it (ready-made version) so that your local package library isn’t copied into the image:.dockerignore
renv/library/ renv/staging/ .Rproj.user/ .git/Build the image:
docker build -t england-prediction .Run it, mounting your
outputsfolder so the figure appears locally:# macOS / Linux docker run --rm -v "$(pwd)/outputs:/project/outputs" england-prediction # Windows PowerShell docker run --rm -v "${PWD}/outputs:/project/outputs" england-prediction