Lesson 1 of 17 · Week 01 · Thu Sep 24

Set Up Your Machine for This Course

Questions

Objectives

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.

  1. Go to cloud.r-project.org.
  2. macOS: choose Download R for macOS and pick the installer that matches your chip (Apple silicon arm64 for M1/M2/M3/M4 Macs, x86_64 for older Intel Macs; check Apple menu → About This Mac).
    Windows: choose Download R for Windows → base.
  3. 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.

  1. Create a folder somewhere easy to find, e.g. Documents/stat-course. Avoid spaces and accents in the name.
  2. In Positron: File → Open Folder…, and choose it.
  3. 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

  1. File → New File… → R File, and save it as scripts/setup-check.R.
  2. Type 1 + 1 on the first line.
  3. 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] 2 appears.

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:

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 used

Click 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/.

Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Exercise 2 — Absolute to relative

0 / 10 attempts used

Click 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.

Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Exercise 3 — Save a download into data/

0 / 10 attempts used

This 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.

Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Exercise 4 — Look before you load

0 / 10 attempts used

A 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 (use file.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.

Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Exercise 5 — Where am I?

0 / 10 attempts used

In 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.

  1. Store getwd() in start.
  2. Move into data with setwd("data") and store the new getwd() in inside.
  3. Move back up one level with setwd("..").
Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Key Points

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.