Lesson 1 of 17 · Week 01 · Thu Sep 24
Set Up Your Machine for This Course
Questions
- What do I need to install to work in R on my own computer?
- How do I organise the files for a project?
- What is a working directory, and why do relative paths matter?
Objectives
- Install R and Positron and confirm Positron finds R.
- Open a course folder as a Positron workspace with
data/,scripts/andresults/. - Install and load packages.
- Tell absolute from relative paths and inspect a file before loading it.
Before you start
Everything in the exercise boxes on this site runs R inside your browser, so you can practise anywhere. Real projects, though, happen on your own computer: your data lives there, and your reports get rendered there. This lesson gets your machine ready for the rest of the course. Most of it is a checklist to work through on your laptop, followed by a few short exercises about folders and file paths.
Plan for about 45 minutes. Installers can be slow. If something fails, write down the exact error message and bring it to class. Error messages are information, not a verdict on you.
Adapted from Software Carpentry's “Project Management With RStudio” episode (CC BY 4.0, The Carpentries). Text, examples and exercises have been rewritten for this course.
Step 1 — Install R
R is the language; everything else in this course sits on top of it.
- Go to cloud.r-project.org.
- macOS: choose Download R for macOS and pick the installer that matches your chip
(Apple silicon
arm64for M1/M2/M3/M4 Macs,x86_64for older Intel Macs; check Apple menu → About This Mac).
Windows: choose Download R for Windows → base. - Run the installer and accept the defaults.
If you already have R, make sure it is version 4.3 or newer. You'll check this in Step 6.
Step 2 — Install Positron
Positron is the editor we use in this course: a data-science IDE from Posit that runs R (and Python). Download the installer for your system, install it, and open it.
The first time Positron starts it looks for R on your computer. At the top right of the window you'll see the interpreter picker. Make sure it shows an R version (for example R 4.5.1). If it shows Python or nothing, click it and choose R. If R isn't in the list, restart Positron after installing R.
Positron ships with Quarto support, which we'll use in Week 08 to write reports. You don't need to install anything extra for that now.
Step 3 — Make a course folder and open it
Positron doesn't use RStudio's .Rproj project files. Instead, a project is simply a folder
that you open with File → Open Folder…. Positron treats that folder as your workspace: the Explorer
on the left lists its files, and the R session it starts uses that folder as its working directory.
- Create a folder somewhere easy to find, e.g.
Documents/stat-course. Avoid spaces and accents in the name. - In Positron: File → Open Folder…, and choose it.
- Inside it, create three sub-folders (right-click in the Explorer → New Folder):
stat-course/
├── data/ # raw data, exactly as you received it; never edited by hand
├── scripts/ # your .R files
└── results/ # anything your scripts create: figures, tables, cleaned data
Keeping raw data separate from generated output means you can always delete results/ and rebuild it from
data/ + scripts/. That is the core of reproducible work. Next time, reopen the same folder
(File → Open Recent) and everything is where you left it.
Step 4 — Run your first line of code
- File → New File… → R File, and save it as
scripts/setup-check.R. - Type
1 + 1on the first line. - With the cursor on that line press Ctrl+Enter (Windows) or Cmd+Enter (macOS).
The line is sent to the Console at the bottom, and
[1] 2appears.
That's the whole workflow of the course: write code in a script, send it to the console, read the output. Code typed straight into the console works too, but it isn't saved anywhere, so put anything you want to keep in a script.
Step 5 — Install the packages we'll use
Packages add functions to R. You install a package once per computer and load it with
library() in every session that needs it. Paste this into the console (just this once;
we don't keep install commands in scripts):
install.packages(c("ggplot2", "dplyr", "tidyr", "readr", "quarto"))
It may take a few minutes and print a lot of text. That's normal. If R asks whether to install from source, answer
no.
Step 6 — Verify your setup
Replace the contents of scripts/setup-check.R with the following, save it, and run the whole file with
Ctrl/Cmd+Shift+Enter (or the Run ▷ button at the top right of the editor):
R.version.string # should say 4.3 or newer
getwd() # should end in your course folder
dir() # should list data, results, scripts
library(ggplot2)
library(dplyr)
library(tidyr)
library(readr)
packageVersion("ggplot2")
Checklist. Tick each one off:
- ☐ R 4.3 or newer is installed.
- ☐ Positron opens and shows R in the interpreter picker.
- ☐ Your course folder is open in Positron and contains
data/,scripts/,results/. - ☐
getwd()prints the path to that folder. - ☐ All four
library()lines run without “there is no package called …”.
Bring to class: a screenshot of the console output from this script. If any line failed, bring the error message too.
Paths: absolute vs. relative
An absolute path spells out the whole location, e.g. /Users/ana/Documents/stat-course/data/rain.csv.
It only works on Ana's computer. A relative path starts from the working directory: data/rain.csv.
Because Positron sets the working directory to the folder you opened, the relative path works for anyone who opens
your course folder, on any computer.
Two functions help you check where you are: getwd() prints the working directory, and dir()
(or list.files()) lists what's in it. You can move with setwd(), but don't put
setwd() in scripts you share: it hard-codes a location that only exists on your machine. Open the right
folder instead.
A quick look at a data file before loading it saves surprises. file.size() reports its size in bytes,
and readLines() reads it as plain lines of text, so length(readLines(f)) counts lines.
In a CSV the first line is usually the header, so the number of data rows is one less.
Exercises
Each exercise gives you up to 10 checked attempts. Use Run as many times as you like to experiment first, then click Check answer when you're ready. Your exercises use numbers/subsets unique to you, so answers won't match a classmate's exactly.
Exercise 1 — Sort files into folders
0 / 10 attempts usedClick Run to see a list of files, stored in files, that a classmate dumped into one folder.
Create three character vectors, to_data, to_scripts and to_results, saying which
file belongs in which folder of the layout above. Every file goes in exactly one place. Remember: raw
inputs go in data/, and anything a script produces goes in results/.
Exercise 2 — Absolute to relative
0 / 10 attempts usedClick Run. You'll see project, the folder a classmate opened in Positron, and
full_path, the absolute path to a data file inside it. Store the relative path to the same
file (relative to project) in a variable called rel_path.
Exercise 3 — Save a download into data/
0 / 10 attempts usedThis exercise has its own small pretend file system. A CSV was just downloaded to the downloads/ folder
(run dir("downloads") to see its name). Create a data folder with
dir.create("data"), then copy the file into it with
file.copy(from = "downloads/NAME", to = "data/NAME"), using the real file name.
Exercise 4 — Look before you load
0 / 10 attempts usedA file data/countries.csv exists in this exercise's pretend file system (its size differs for every
student). Without loading it as a table, store:
- its size in bytes in
n_bytes(usefile.size()), and - the number of data rows (lines minus the header line) in
n_rows.
Print the first few lines with head(readLines("data/countries.csv")) to see what kind of values it holds.
Exercise 5 — Where am I?
0 / 10 attempts usedIn this exercise's pretend file system, the working directory contains a data folder. Practise moving
around. At the end, you must be back where you started.
- Store
getwd()instart. - Move into
datawithsetwd("data")and store the newgetwd()ininside. - Move back up one level with
setwd("..").
Key Points
- In Positron, a project is a folder you open with File → Open Folder; its R session starts in that folder.
- Keep raw data in
data/, code inscripts/, and generated output inresults/. - Use relative paths like
data/rain.csv; avoidsetwd()in shared scripts. - Install a package once with
install.packages(); load it every session withlibrary().
Proof of completion
Once you've attempted every exercise above, download a PDF summary of your work on this lesson — your name, the exercises you passed, attempt counts, timing, and your final code for each.