Introduction to Paths
- To use data stored on a computer we need to tell R where it is
- This is done using paths
- Paths can be absolute
shrub_data <- read.csv('/home/ethan/datacarp/data/shrub-dimensions-labeled.csv') # OSX/Linux
shrub_data <- read.csv('/Users/datacarp/data/shrub-dimensions-labeled.csv') # Windows
- Folders/Directories are separate by
/
with the file name at the end - Paths can also be relative
shrub_data <- read.csv('data/shrub-dimensions-labeled.csv')
- “From where I am open the shrub-dimensions file from the data subdirectory”
- Absolute & relative paths are the same if R thinks it’s in
/home/ethan/datacarp
Find out where you are
- To find out where R is use
getwd()
getwd()
- “get working directory”
-
The “working directory is where the program starts from
- See what is in the working directory using
list.files()
list.files()
Loading data
- For data in the working directory just use the file name
shrub_data <- read.csv('shrub-dimensions-labeled.csv')
-
Download a file into working directory using
download.file()
- For data not in the working directory - two options
- tell R where it is
- change the working directory to where it is
- Changing the working directory is common
- Who here uses
setwd
regularly? - Does that ever cause any issues?
- Working on a different computer?
- Working with someone else’s files?
- Who here uses
- Have working directory automatically set to a common place and use relative paths
Projects
- Think of each project as a self-contained unit in a single folder/directory
- Treat all locations as relative to that single directory
- To do this in RStudio we use projects
File
->New Project
->New Directory
->New Project
-> datacarp- Or use
Existing Directory
to choose an existing directory - Creates .Rproj file
- Isn’t project itself
- Contains project info
- Don’t change manually
- Can switch between projects using
File
->Recent Projects
orOpen Project
- Keeps track the state of RStudio when you last worked with that project
- Create a project for this assignment
- Do Exercise 9 - Shrub Volume Data Frame.
Assignments
- This idea of projects as folders is also important for how we share code
- To turn in assignments you will submit a compressed version of the folder for that assignment
- Demo compressing a folder
- Version control also treats projects as folders
Assign remaining exercises. Submit as compressed main folder, see Assignment Submission & Checklist