Day 2 — Folders and paths
Lecture slides · Quiz · Weekly revision
Where is yesterday’s note?
Open the file manager and find day01-notes.txt by opening folders, not through the editor’s list of recent files, and write down the folders you opened, in order. That list is the note’s address. Today we learn to write such addresses down precisely, so that you, a classmate and R can all find the seedling measurements.
The project as a tree
A folder can contain files and more folders, so a project can be drawn as a tree in which indentation shows what is inside what:
introcomputing-course/
├── introcomputing.Rproj
├── report.Rmd
├── data/
│ ├── README.md
│ └── seedlings.csv
├── examples/
├── output/
│ └── seedlings-practice.csv
└── reports/
├── day01-notes.txt
└── file-types.txt
The drawing leaves out most supplied files. data and reports are neighbours: neither is inside the other, and their parent folder is introcomputing-course. That folder is the project folder (also called the project root), the starting point for everything in the project.
In the file manager, open reports, go up to its parent and enter data, and watch the address bar in Windows File Explorer. In Finder, choose View → Show Path Bar to see the route at the bottom of the window.
Paths
A path lists the folders on the route to a file, separated by / (or \ on Windows), and ends with the filename. Your username and the location of Documents will differ from these examples:
Windows: C:\Users\anna\Documents\introcomputing-course\data\seedlings.csv
Mac: /Users/anna/Documents/introcomputing-course/data/seedlings.csv
Linux: /home/anna/introcomputing-course/data/seedlings.csv
Read a path from left to right: start at the top of the disk (/, or the drive C:\ on Windows) and open each folder in turn.
Find out the full path of seedlings.csv on your own laptop. On Windows, select the file and choose Copy as path; on Mac, right-click it, hold the Option key and choose Copy “seedlings.csv” as Pathname. Paste the path into a text file. It should end in introcomputing-course/data/seedlings.csv, with your own username near the start.
Absolute and relative paths
The paths above are absolute: they start at the top of the disk and give the whole route. A relative path starts from a folder you have agreed on. From the project folder, data/seedlings.csv means “enter data, then find seedlings.csv”. From inside reports, the same words ask for a data folder inside reports, which does not exist. The folder from which a program reads relative paths is its working directory; tomorrow the terminal will tell us its working directory, and later RStudio will start in the project folder.
The absolute path contains your username and the place you chose for the folder, so it differs on every laptop. The relative path data/seedlings.csv is the same for everyone who has the project folder, which is why our R code will use relative paths.
Finding files from different starting folders
Use the file manager to find out which route leads from reports to the practice copy output/seedlings-practice.csv. You have to go up to the project folder first, because output is a neighbour of reports, not inside it.
Then make a place for notes: create a folder practice in the project folder, with field-notes and figures inside it, and copy yesterday’s note into field-notes under the name seedling-observations.txt. Write down its relative path from the project folder, close the file manager, and follow your path to open the note again. The path is practice/field-notes/seedling-observations.txt.
Names made of simple letters, numbers and hyphens, such as seedling-observations.txt, say what a file holds and are easy to type in the terminal and in R. Spaces and å, ä, ö cause trouble there.
Saved and unsaved work
While you type, the text lives in the computer’s working memory, which is cleared when the program closes. Saving writes the text to a file on the disk, where it stays after the program closes or the computer is switched off. Use the copy practice/field-notes/seedling-observations.txt to see the difference. Add “The plants were measured twice.”, save, close and reopen: the sentence is there. Then add “This line is not saved.”, close the editor and choose Don’t save: after reopening, that line is gone. Some editors save automatically or restore unsaved text; what counts is what is in the saved file.
Files on other computers
A path describes a place on one computer. A cloud folder (OneDrive, iCloud) may keep a copy elsewhere and synchronise it. Later in the course you will log into a remote Linux computer whose folders are separate from your laptop’s, even if the names look the same, so a file is there only after you have copied it there. For the same reason, a second copy on the same laptop is not a backup: keep important work in the university’s supported storage as well.
What happens to a path when a folder moves?
Use the practice folder to find out which written routes break when a folder is moved. Create practice/map-a and practice/map-b, and put a copy of your note in map-a as note.txt. In a new plain-text file reports/day02-map.txt, write two routes to note.txt: one from the project folder, and one from inside map-b, which must first go up to practice. Write down how you expect both routes to change, then move map-a into map-b with the file manager, follow the routes and correct them. Open note.txt to see whether its contents changed.
Finally, add to reports/day02-map.txt a small tree of the project folder with data, output, reports and practice, and the relative path from the project folder to seedlings.csv.
From inside map-b, the route to map-a goes up one level to practice and then down into map-a. In the terminal tomorrow, “up one level” is written ...
Before the move, the note is at practice/map-a/note.txt from the project folder, and at ../map-a/note.txt (up to practice, then into map-a) from inside map-b. After the move, it is at practice/map-b/map-a/note.txt from the project folder and at map-a/note.txt from inside map-b. The contents of note.txt are unchanged; only its location changed. The path to the measurements is data/seedlings.csv.
Check your understanding
reports/day01-notes.txt. From inside reports, its name alone is enough, day01-notes.txt, because the starting point changed while the file stayed put.
data/seedlings.csv rather than C:\Users\anna\…?
The absolute path works only on Anna’s laptop. The relative path works on any computer that has the project folder, as long as R starts in that folder.
Save today: reports/day02-map.txt and the practice folder.