File management

Reading and writing files in R

Keeping track of your data

If you’re working with data you need to be able to keep track of where you store it, especially if it concerns sensitive data such as personal information. In this section we’ll first explain and demonstrate a simple, recommended approach to managing files in R. Secondly, we provide some optional material that goes more in-depth on how your computer’s file system works, and we provide some additional techniques that give you more flexibility in managing files. This second part is optional, and if you’re just starting out with R we recommend skipping it for now.

Optional: working with absolute paths

In the previous section we showed you the easiest way to manage files in R: by sticking to your working directory. This will get you through most situations, but sometimes it’s convenient to be able to read files from other locations on your computer. You can achieve this by specifying the complete path to the file, also called the absolute path (as opposed to the relative path we used before). If we know that the path to our Desktop is /home/kasper/Desktop, we can write a file directly to the Desktop with the following code:

writeLines("Hello desktop!", "/home/kasper/Desktop/hello_desktop.txt")

Notice that the absolute path must start with the root directory, which is / on Linux and Mac, and C:\ (or another drive) on Windows.

Off course, if you run the above line of code it will throw an error saying that there is No such file or directory (unless you happen to be another user named kasper working on a Linux machine). So in order to work with absolute paths, you’ll need to know how to find the path to a file or directory on your computer.

Computers organize files and directories (or folders) in a hierarchical structure that resembles a tree, called the file system. This tree-like structure starts from a single root directory and branches out into subdirectories, which can contain more subdirectories or files. It looks a bit different on Windows and Mac (or Linux), but the basic idea is the same. Here is a simplified example (normally there are more layers, such as your user directory):

/ (Root Directory) 

├── Documents
│   ├── Work
│   │   ├── survey.csv
│   │   └──  
│   └── Personal
│       ├── Resume.pdf
│       └── Budget.xlsx

├── Downloads
│   ├── Report.docx
│   └── image.jpg

Because of this structure, any file on your computer has a unique path that describes its location in the file system. For example, the path to the Report.docx file in the Work directory would be /Documents/Work/survey.csv, or C:\Documents\Work\survey.csv on Windows.

Finding the absolute path

So how do you find the absolute path to the file? Here are several options.

Using file.choose()

If you run the code file.choose(), R will open a file explorer window.

file.choose()              ## find path of existing file
file.choose(new = TRUE)    ## create a path for a new file

Here you can browse to the file, and when you select it, R will print the file path in the console window. This is easy, because you get a nice menu to browse through your files. But it’s also a bit cumbersome, because you have to do it every time you want to read a file. Also, you can’t select folder, only existing files.

Using the file explorer

You can also use your own file explorer (outside of RStudio). Navigate to a file or folder, and right click on it. You should then be able to select something like Properties (this depends on your operating system). Here you should be able to find the absolute path.

Using tab completion

There is one other nice trick that you can use to find files on your computer: tab completion. Whenever you are writing something in a code editor, you can often use the Tab key (above caps lock) to automatically complete it (like auto-complete on your phone). If you’ve ever seen programmers work really fast, it’s because they’re using tab completion all the time.

To use tab completion for file paths, put your text cursor between the quotes in readLines("") (or any function for reading/writing files), and then press the Tab key. If there are multiple files that match the characters you’ve typed so far, RStudio will show you all the options. Keep typing to narrow down the options, and once you see the file or directory you want, press tab again to complete it. This takes some time to get used to, but it’s a very powerful trick.

Back to top