Appendix A — Navigating the file system

It is essential to know how R interacts with the file system on your computer. Modern operating systems are incredibly user-friendly and try to hide boring and annoying stuff from the customer. In the following, I will try to give a brief introduction on how to navigate around a computer using a DOS or UNIX shell. If you familiar with that, you can skip this part of the notes.

A.1 The file system

In this section, I describe the basic idea behind file locations and file paths. Regardless of whether you are using Windows, macOS, or Linux, every file on the computer is assigned a human-readable address, and every address has the same basic structure: it describes a path that starts from a root location, through a series of folders (or directories), and finally ends up at the file.

On a Windows computer, the root is the storage device on which the file is stored, and for many home computers, the name of the storage device that stores all your files is C:. After that comes the folders, and on Windows, the folder names are separated by a backslash symbol \. So, the complete path to this book on my Windows computer might be something like this:

C:\Users\huber\Rbook\rcourse-book.pdf

On Linux, Unix, and macOS systems, the addresses look a little different, but they are more or less identical in spirit. Instead of using the backslash, folders are separated using a forward slash, and unlike Windows, they do not treat the storage device as being the root of the file system. So, the path on a Mac might be something like this:

/Users/huber/Rbook/rcourse-book.pdf

That is what we mean by the path to a file. The next concept to grasp is the idea of a working directory and how to change it. For those of you who have used command-line interfaces previously, this should be obvious already. But if not, here is what I mean. The working directory is just whatever folder I am currently looking at. Suppose that I am currently looking for files in Explorer (if you are using Windows) or using Finder (on a Mac). The folder I currently have open is my user directory (i.e., C:\Users\huber or /Users/huber). That is my current working directory.

A.2 Working directory

The next concept to grasp is the idea of a working directory and how to change it. For those of you who have used command line interfaces previously, this should be obvious already. But if not, here’s what I mean. The working directory is just “whatever folder I’m currently looking at”. Suppose that I’m currently looking for files in Explorer (if you’re using Windows) or using Finder (on a Mac). The folder I currently have open is my user directory (i.e., C:\Users\huber or /Users/huber). That’s my current working directory.

The fact that we can imagine that the program is “in” a particular directory means that we can talk about moving from our current location to a new one. What that means is that we might want to specify a new location in relation to our current location. To do so, we need to introduce two new conventions. Regardless of what operating system you’re using, we use . to refer to the current working directory, and .. to refer to the directory above it. This allows us to specify a path to a new location in relation to our current location, as the following examples illustrate. Let’s assume that I’m using my Windows computer, and my working directory is C:\Users\huber\Rbook. The table below shows several addresses in relation to my current one:

Absolute path Relative path
C:\Users\huber ..
C:\Users ..\..
C:\Users\huber\Rbook\source .\source
C:\Users\huber\nerdstuff ..\nerdstuff

It is quite common on computers that have multiple users to define ~ to be the user’s home directory. The home directory on a Mac for the `huber'' user is/Users/huber/. And so, not surprisingly, it is possible to define other directories in terms of their relationship to the home directory. For example, an alternative way to describe the location of thercourse-book.pdf` file on a Mac would be

~\Rbook\rcourse-book.pdf

You can find out your home directory with the path.expand() function:

path.expand("~")
[1] "/home/sthu"

Thus, on my machine ~ is an abbreviation for the path /home/sthu.

getwd()
[1] "/home/sthu/Dropbox/hsf/courses/dsr"

A.3 Navigating the file system using the R console

When you want to load or save a file in R it’s important to know what the working directory is. You can find out by using the getwd() command. For the moment, let’s assume that I’m using Mac OS or Linux, since things are different on Windows, see section Section A.5. Let’s check the current active working directory:

getwd()
[1] "/home/sthu/Dropbox/hsf/courses/dsr"

The function setwd() allows to change the working directory:

setwd("/Users/huber/Rbook/data")
setwd("./Rbook/data")

The function list.files() lists all the files in that directory:

list.files()

A.4 R Studio projects

Setting the working directory repeatedly can be a cumbersome task. Fortunately, R Studio projects can automate this process for you. When you open an R Studio project, the working directory is automatically set to the project directory.

Creating a new project in R Studio is simple. Just click on File > New Project…. This will create a directory on your computer with a *.Rproj_ file that can be used to open the saved project at a later date. The newly created directory contains your R code, data files, and other project-related files. By working within projects, all of your files and data are organized in one place, making it easier to share your work with others, reproduce your analyses, and keep track of changes over time.

A.5 Why do the Windows paths use the back-slash?

Let’s suppose I’m using a computer with Windows. As before, I can find out what my current working directory is like this:

getwd()
[1] "C:/Users/huber/

R is displaying a Windows path using the wrong type of slash, the back-slash. The answer has to do with the fact that R treats the \ character as special. If you’re deeply wedded to the idea of specifying a path using the Windows style slashes, then what you need to use two back-slashes \\ whenever you mean \. In other words, if you want to specify the working directory on a Windows computer, you need to use one of the following commands:

setwd( "C:/Users/huber" )
setwd( "C:\\Users\\huber" )