12 File paths

In this part of the book, we will need to work with file paths. File paths are nothing more than directions that tell R where to find, or place, data on your computer. In my experience, however, some students are a little bit confused about file paths at first. So, in this chapter I will briefly introduce what file paths are and how to find the path to a specific file on your computer.

Let’s say that I want you to go to the store and buy a loaf of bread.

When I say “go to the store,” this is really a shorthand way of telling you a much more detailed set of directions.

Not only do you need to do all the steps in the directions above, but you also need to use the exact sequence above in order to arrive at the desired destination.

File paths aren’t so different. If we want R to “go get” the file called my_study_data.csv, we have to give it directions to where that file is located. But, the file’s location is not a geographic location that involves making left and right turns. Rather, it is a location in your computer’s file system that involves moving deeper into folders that are nested inside one another.

For example, let’s say that I have a folder on my desktop called “NTRHD” for "North Texas Regional Health Department.

And, my_study_data.csv is inside the NTRHD folder.

I can give R directions to that data using the following path:

/Users/bradcannell/Desktop/NTRHD/my_study_data.csv (On Mac)


C:/Users/bradcannell/Desktop/NTRHD/my_study_data.csv (On Windows)

⚠️Warning: Mac and Linux use a forward slashes in a file paths (/) by default. Windows uses a backslashes (\) in file paths by default. However, no matter which operating system you are using, you should still use forward slashes in the file paths you pass to import and export functions in RStudio. In other words, use forward slashes even if you are using Windows.

These directions may be read in a more human-like way by replacing the slashes with “and then.” For example, starting a the computer’s hard drive (this is typically the C drive on a Windows) go into files that are accessible to the username bradcannell, and then go into the folder called Desktop, and then go into the folder called NTRHD, and then get the file called my_study_data.csv.

⚠️Warning: You will need to change bradcannell to your username, unless your username also happens to be bradcannell.

⚠️Warning: Notice that we typed .csv at the end immediately after the name of our file my_study_data. The .csv we typed is called a file extension. File extensions tell the computer the file’s type and what programs can use it. In general, you MUST use the full file name and extension when importing and exporting data in R.

Self Quiz:

Let’s say we moved my_study_data.csv to a different folder on my desktop called research. What file path would we need to give R to tell it how to find the data?

/Users/bradcannell/Desktop/research/my_study_data.csv (On Mac)


C:/Users/bradcannell/Desktop/research/my_study_data.csv (On Windows)

Now let’s say that we created a new folder inside of the research folder on my desktop called my studies. Now what file path would we need to give R to tell it how to find the data?

/Users/bradcannell/Desktop/research/my studies/my_study_data.csv (On Mac)


C:/Users/bradcannell/Desktop/research/my studies/my_study_data.csv (On Windows)

12.1 Finding file paths

Now that you know how file paths are constructed, you can always type them manually. However, typing file paths manually is tedious and error prone. Luckily, both Windows and MacOS have shortcuts that allow you to easily copy and paste file paths into R.

On a Mac, right-click on the file you want the path for and a dropdown menu will appear. Then, click the Get Info menu option.

Now, just copy the file path in the Where section of the get info window and paste it into your R code.

A similar method exists in Windows as well. First, hold down the shift key and right click on the file you want the path for. Click Copy as path in the dropdown menu that appears and paste the file path into your R code.

Now that we know what file paths are and how to find them, let’s use them to import and export data to and from R.