%%{init: {'theme':'base', 'themeVariables':{'primaryColor':'#ffffff', 'primaryBorderColor': '#000000'}}}%% graph TD; A[Hard drive] --> B["π Operating System"]; A --> C["π Users"]; C --> D["π damonroberts"]; C --> E["π guest"];
In an age of cloud and mobile access, many of us have lost sight of the fact that files have to be stored somewhere. As we produce a lot of files, we need to make sure that they are relatively organized in some coherent way.
Just as we organize our closets (or not), we want to find particular articles of our clothes somewhat easily. We put our pants in one area, our t-shirts in another, our workout clothes in some other spot. Computers need to do this as well.
Everything on our computer is located on a Hard Drive. This hard drive contains everything about our computer. Our hard drives are separated up into different components. Some areas of our hard drive are dedicating to storing the information for our operating system (yes, operating systems are just collections of files). Other areas are dedicated to files for the Users of the computer. If there are multiple users, that section of our computer is separated even more. We separate our hard drives by placing things in folders. So from what Iβve discussed so far, this is one way to visualize this:
%%{init: {'theme':'base', 'themeVariables':{'primaryColor':'#ffffff', 'primaryBorderColor': '#000000'}}}%% graph TD; A[Hard drive] --> B["π Operating System"]; A --> C["π Users"]; C --> D["π damonroberts"]; C --> E["π guest"];
For each user, we often have our own documents, applications (software), downloads, and other things that we want access to.
%%{init: {'theme':'base', 'themeVariables':{'primaryColor':'#ffffff', 'primaryBorderColor': '#000000'}}}%% graph TD; A[Hard drive] --> B["π Operating System"]; A --> C["π Users"]; C --> D["π damonroberts"]; C --> E["π guest"]; D --> F["π Desktop"]; D --> G["π Documents"]; D --> H["π Downloads"];
We often have particular files or more folders inside these! Say I download a file called psci_2075.pdf
. Where would it probably go on my computer? Thatβs right, probably in π Downloads
(unless Iβve set it up to be something else on my computer).
So how would the location of that file appear relative to my hard drive?
%%{init: {'theme':'base', 'themeVariables':{'primaryColor':'#ffffff', 'primaryBorderColor': '#000000'}}}%% graph TD; A[Hard drive] --> B["π Operating System"]; A --> C["π Users"]; C --> D["π damonroberts"]; C --> E["π guest"]; D --> F["π Desktop"]; D --> G["π Documents"]; D --> H["π Downloads"]; H --> I["π psci_2075.pdf"];
Working directories refer to the default location that we are working from. For many of us, our working directory is at our particular π Users
folder. So for my computer, it is π damonroberts
.
Why do we rely on a default location? Just because it is easier for us, the user, to reference anything relative to the location of our Users
π.
Why do we need to know the location of a file? Because we may have a file called psci_2075.pdf
somewhere else on our computer. We need to be sure that our computer knows which one we are referring to. To do this, our computer needs to know the file path.
So the file path to π psci_2075.pdf
would be:
Users/damonroberts/Downloads/psci_2075.pdf
C:\Users\damonroberts\Downloads\psci_2075.pdf
home/damonroberts/Downloads/psci_2075.pdf
Notice how we can essentially follow that file tree down to access our file?
While Windows specifically requires that you specify the hard drive (C:
) for your computer at the start, the rest of the information is the same.
Think of the file name as the specific apartment or house number. The rest of the file path would be information like the Street, City, State, and Zip Code. It provides a precise location as to where you are. If you only gave someone your apartment number to deliver a package, do you think that they would have much luck delivering it to the right apartment? Probably not. Same with accessing files on your computer.
In R, you need to access files through file paths. You canβt just click on a file to interact with it in R. You need to provide a file path to R so that it knows precisely what file you are talking about on your computer. You will see why this is important later on.
Why should I know about working directories? Well, R uses working directories to help make your life easier. Rather than having to write out the entire file path each time you want to interact with a particular file in R, instead you can just write out, in reference to your working directory, where that file is.
Say that you want to access psci_2075.pdf
π on your computer and your working directory is at your particular Users
folder (working directory: /Users/damonroberts/
). What would the file path be?
./Downloads/psci_2075.pdf
./
refers to the current working directory/Users/damonroberts/Downloads/psci_2075.pdf
right-click
anywhere on your wall paper.Create New Folder
.psci_2075
This means that your file tree would look something like this:
%%{init: {'theme':'base', 'themeVariables':{'primaryColor':'#ffffff', 'primaryBorderColor': '#000000'}}}%% graph TD; A[Hard drive] --> B["π Operating System"]; A --> C["π Users"]; C --> D["π damonroberts"]; C --> E["π guest"]; D --> F["π Desktop"]; D --> G["π Documents"]; D --> H["π Downloads"]; F --> I["π psci_2075"];
getwd()
psci_2075
folder, at the top of each .R
or .qmd
file you write, put:setwd("./Desktop/psci_2075")
psci_2075
π.