"cannot open file" means the wrong folder
◈ 8 cardsorders <- read.csv("orders.csv") reads the 12-row table with header = TRUE and sep = "," by default. "cannot open file 'orders.csv': No such file or directory" is a working-directory problem — getwd(), setwd(), or an RStudio Project. write.csv(agg, "agg.csv", row.names = FALSE) — forget the argument and a junk first column appears.
The line that starts every script
Maple & Birch's January orders are in orders.csv — one header row and twelve data rows, comma-separated. One call reads it into a data frame:
> orders <- read.csv("orders.csv")
> orders
order_id prov channel amount units order_date
1 1001 ON Online 420 3 2025-01-06
2 1002 QC Store 180 1 2025-01-08
3 1003 ON Store 760 5 2025-01-15
4 1004 BC Online 250 2 2025-01-21
5 1005 AB Online 1320 8 2025-02-03
6 1006 ON Online 510 4 2025-02-10
7 1007 QC Online 90 1 2025-02-14
8 1008 BC Store 640 4 2025-02-27
9 1009 ON Store 305 2 2025-03-04
10 1010 AB Store 980 6 2025-03-11
11 1011 QC Store 215 2 2025-03-19
12 1012 BC Online 470 3 2025-03-25
This is the frame every lesson from here to Module 14 uses. read.csv() has three defaults worth knowing, all visible in args(read.csv): header = TRUE (the first row becomes the column names), sep = "," (the separator), and — since R 4.0.0 — text stays chr. For a semicolon-separated European export, read.csv2() or sep = ";".
Worked example — the error everyone meets first
Run the same line from the wrong folder:
> orders <- read.csv("orders.csv")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'orders.csv': No such file or directory
The file is fine. The CSV is fine. R is fine. R looked for orders.csv in its working directory — the folder it treats as "here" — and the file is somewhere else. Three fixes, in order of preference:
- An RStudio Project. File → New Project in the folder that holds the data; every script in the project then opens with that folder as the working directory.
getwd()prints the current working directory;setwd("C:/Users/you/afm112")changes it. On Windows, write the path with forward slashes or doubled backslashes — a single\is an escape character inside a string.file.choose()opens a file picker and returns the full path:read.csv(file.choose()).
The message to memorise is the warning's last line: "cannot open file … No such file or directory" = the path, not the file.
Writing a result back
After Module 12's province summary, agg holds four rows. write.csv() saves it — but its default row.names = TRUE writes the row names as a first column with an empty header:
"","prov","total"
"1","AB",2300
"2","BC",1360
Open that in Sheets and column A is junk. The habit:
> write.csv(agg, "agg.csv", row.names = FALSE)
"prov","total"
"AB",2300
"BC",1360
Type the read and the write
The questions cover the error's cause, the separator default, and the missing row.names argument.
CRISP-DM: read.csv() is data understanding → collect initial data; write.csv() is deployment → produce final report.