4  Path and folders

Author

Solveig Bjørkholt

Your computer stores everything you work with in folders (also called “directories”). If you open File Explorer on Windows or Finder App on Mac, you’ll see the archive that your computer is storing for you. The archive has an arrangement which makes it easy to search through your files – a tree-like structure. You have a folder, and within that folder you can have another folder, and within that folder, another folder, until there is a file. This tree-structure, from root to branch, is often called a “path”. For example, the path to your “myscript.R” could look something like the structure below:

Here, C-disk is the root and the file is myscript.R. The path would be: C-disk/Users/School/R-things/myscript.R

This is important, because as with any archive, you need to know where you save things. It’s also very important because when we import things into R, for example a dataset, you need to tell the computer where to find the file with that dataset. We do that by specifying the path to the file. For example, if we would want to read data.csv into R, the code would be:

dataset <- read_csv("C-disk/Users/School/R-things/data.csv")

Notice that in the code above, read_csv is the function that reads a dataset into R, the argument is the path to the file, and we assign the dataset to an object called “dataset” by the dataset <- part.

In RStudio, the window in the right hand corner mirrors the folders on your computer under the tab “Files”. You can make adjustments here just as you can in your folder structure.