94 here and filepaths

Written by Matthew Wankiewicz and last updated on 7 October 2021.

94.1 Introduction

In this lesson, you will learn how about:

  • Filepaths and the here package

Highlights:

  • here allows you to reference filepaths in a more replicable way.
  • If you have the option to use file.path vs here, here is generally the better option to use.

94.2 The content

When working on projects using R, filepaths are going to be an important part of calling datasets or saving files. Filepaths are important because when working in R projects, you will likely have multiple folders which contain both R scripts for cleaning or collecting data and also R markdown files or Shiny files to display your data. This section will look at how to properly use filepaths in R and will also introduce the here package which makes it easier to find your files.

94.2.1 Filepaths

Filepaths for files like .csv’s in R will look like ../data/file.csv. The second part will change depending on what your data folder is named and the file name will change depending on what you have named your file. Filepaths can be typed out but the process can be shortened by using the file.path() function. This function takes in strings for arguments and will create a file path using those strings.

For example, if I have a .csv called ‘2021data’ in a folder called ‘data,’ if I write file.path("data", "2021data.csv") the function will create the file path data/2021data.csv. This file path can then be used to read in the file to bring the data into the file currently being worked on. This function will also work for other data types like .rds or .txt.

94.2.2 Here

The here package was created to make the finding of filepaths simpler. The main function in the package is here(), and is meant to be used as a replacement for the file.path() function we discussed earlier. here is good to use because it creates these filepaths relative to the project you are currently working in, helping you avoid any errors.

Using the here function is identical to the use of file.path. Simply enter the name of the folder(s) and file you are trying to reach and the function will create a filepath through your current project to get to the desired file. This function can also be used to see what your current directory is.

Another benefit to the here package is that the filepaths it creates are accessible for people using different operating systems. This means that if I use the function on a Mac, someone using a PC will be able to get the same result. here is also helpful because when working with R projects, you may run into issues where R cannot find a file in a directory you wrote out, but after using here, R will be able to find those files.

94.3 Arguments

  • file.path: This function just takes strings which it then turns into a file path for you.
  • here: Like file.path, the only argument you need is the path you want to take.

The here library also contains another function called i_am. This function tells the here function where to start the file path from. For example, if you are working in an R project and want here to run from a specific folder, it will tell it to start at that folder. - When using i_am be sure to call it on an existing file or you will end up with an error.

94.4 Examples

If we are working on a project which contains a folder called “Output” and a file called “Data_Analysis,” we will find that file.path and here will return different paths.

Firstly, with file.path we will get:

file.path("Output", "Data_Analysis")
#> [1] "Output/Data_Analysis"

Next, when we use here we will get:

here("Output", "Data_Analysis")
#> [1] "/Users/rohanalexander/Documents/projects/doss_toolkit_book/Output/Data_Analysis"

Both function do the same thing, but here just goes a little bit farther in making the path. This is extremely useful when creating projects because it will allow your work to be much more reproducible.

Looking at here::i_am, we can say that we are working in a file called “filepaths.Rmd” and let here decide where to start the filepath from.

## here::i_am("filepaths.Rmd")

The chunk above will return a string saying “here() starts at /Users/name/folder,” depending on the file path your computer takes to get to the file.

94.5 Exercises

To get used to file.path, create the path Users/DoSS/Toolkit using file.path

94.6 Common Mistakes & Errors

  • One common issue highlighted in this lesson was using i_am on files that aren’t in your current project. This can happen from typing a file name wrong but if it didn’t, make sure the file you’re referencing is in your project/directory.
    • An example of this is the error saying: “Could not find associated project in working directory or any parent directory”
  • One of the only errors which occurs with here is “Error in UseMethod.” This only happens when you accidentally reference a function inside your here call.

94.7 Next Steps

Some more information on filepaths and here include:

94.8 Questions

  1. What does the here package do?
  1. It makes it easier to find files you are working with
  2. Changes your R project name
  3. Makes it harder to work on a project
  4. None of the above
  1. What does a file path look like?
  1. data+file.csv
  2. data-file.csv
  3. data%>%file.csv
  4. /data/file.csv
  1. What is the output of file.path("question", "3")?
  1. “question/3”
  2. “question,3”
  3. “question3”
  4. The function call will result in an error
  1. True or False, using the here function is identical to the use of file.path?
  1. True
  2. False
  1. True or False, if I use here on a Mac, someone using the same function call on a PC will get a different result?
  1. True
  2. False
  1. What is returned when you run the i_am function?
  1. The R project name
  2. The filepath leading to the file
  3. Your file’s name
  4. Nothing
  1. What causes the “Error in UseMethod” error?
  1. You likely called a function inside here
  2. It happens randomly without explanation
  3. The UseMethod package is not installed
  4. None of the above
  1. If I have a folder called “notes” that I am working in, what is the output of here("folder", "file")?
  1. “folder/file”
  2. “file”
  3. “…/notes/folder/file”
  4. “notes”
  1. If I have a folder called “notes” that I am working in, what is the output of file.path("folder", "file")?
  1. “folder/file”
  2. “file”
  3. “…/notes/folder/file”
  4. “notes”
  1. True or False, here and file.path work for any file type?
  1. True
  2. False