library(readr)
library(readxl)
library(here)12 RStudio Projects & Data Import
Learning Objectives: After this chapter you will be able to: - Work inside an RStudio Project (
.Rproj) instead ofsetwd(). - Build portable paths withhere::here(). - Read CSV files withreadr::read_csv()and Excel files withreadxl::read_excel().
12.1 Why avoid setwd()
setwd("C:/Users/you/project") hard-codes a path that exists only on your machine. Anyone else who opens the script β or you on a different computer β gets an error. An RStudio Project (.Rproj) fixes this: opening the .Rproj file sets the working directory to the project folder automatically, so relative paths just work.
Workflow: File β New Project β New Directory (or Existing Directory) β give it a name. From then on, open the .Rproj file, not individual scripts.
12.2 Portable paths with here
The here package builds paths from the project root, wherever the project lives on disk:
here("data", "students.csv")[1] "F:/R/ebooks/intro-r/data/students.csv"
Common Mistake: Paths with
../orC:/...break when the project moves. If a path starts with a drive letter or../, rewrite it withhere().
12.3 Reading CSV files
Practice data lives in the data/ directory (CC0 / public domain, created for this book). Read it with read_csv():
students <- read_csv(here("data", "students.csv"))
students# A tibble: 5 Γ 3
name age score
<chr> <dbl> <dbl>
1 Asha 21 88
2 Ravi 22 92
3 Meera 20 79
4 Arjun 23 95
5 Divya 21 84
12.4 Reading Excel files
read_excel() works the same way for .xlsx files β sheet 1 by default:
students_xl <- read_excel(here("data", "students.xlsx"))
students_xl# A tibble: 5 Γ 3
name age score
<chr> <dbl> <dbl>
1 Asha 21 88
2 Ravi 22 92
3 Meera 20 79
4 Arjun 23 95
5 Divya 21 84
Try it in your browser
Edit the code below and press Run. It executes entirely in your browser via WebR β no R installation needed. The runtime downloads once, on this page only.
12.5 Practice Check
Knowledge Check: Project-relative paths Why does read_csv(here("data", "students.csv")) keep working when you move the whole project folder to another computer, but read_csv("C:/Users/you/project/data/students.csv") does not?
Click to reveal answer
Answer: here() resolves the path relative to the project root at run time, so it adapts to the new location. The C:/... path is absolute and points to a folder that does not exist on the other computer.
here() keep every path portable (project root β data β file); absolute paths and setwd() tie the script to one machine.
Knowledge Check: Why .Rproj beats setwd() What breaks when you share a script containing setwd("C:/Users/you/project") with a colleague?
Click to reveal answer
Answer: The script errors because that folder does not exist on your colleagueβs machine.
Explanation:setwd() with an absolute path ties the script to one computer; opening a shared .Rproj file sets everyoneβs working directory to the project folder automatically.
Knowledge Check: here() resolution Your project lives at D:/work/survey and contains data/resp.csv. What does here("data", "resp.csv") return?
Click to reveal answer
Answer: D:/work/survey/data/resp.csv (i.e. project root β data β resp.csv).
here() anchors paths at the project root (where the .Rproj file lives), joining each additional component with the correct separator for your OS.
Knowledge Check: read_csv() vs read.csv() Name one practical advantage of readr::read_csv() over base Rβs read.csv() for a beginner.
Click to reveal answer
Answer: Any one of: it never converts strings to factors, it prints a compact tibble preview, or it reports column types as it reads.
Explanation:read_csv() uses modern defaults (no stringsAsFactors surprises) and tells you the parsed type of each column, so mismatches are visible immediately.
Knowledge Check: Excel sheets read_excel(here("data", "students.xlsx")) reads which sheet by default, and how would you read the second sheet instead?
Click to reveal answer
Answer: The first sheet by default; pass sheet = 2 (or the sheet name) to read the second sheet.
read_excel() defaults to sheet 1; the sheet argument selects another sheet by position or name.