How to Locate R Script and Data Files Without Changing the Working Directory

Jesse John Feb 02, 2024
  1. R Studio Projects
  2. the file.path() Function
  3. the here Package
  4. the Suggested Workflow
How to Locate R Script and Data Files Without Changing the Working Directory

We may often want to set the current file’s location as R’s working directory because it enables us to use relative paths to reference needed R scripts and data files. However, changing the working directory is not an ideal solution.

This article shows how to combine an R Studio Project, the here package and the file.path() function to enable R to locate files needed for a project without manually changing the working directory to the location of the current script.

R Studio Projects

When we create an R Project using R Studio, it creates a .Rproj file in the specified folder.

When we open a .Rproj file:

  1. We get a new instance of R Studio with a workspace dedicated to that project.
  2. The folder in which the .Rproj exists is made the working directory.

However, a .Rmd file in a sub-folder of this project folder will temporarily change the working directory to its folder when it is knit. The here package mentioned below solves this problem.

the file.path() Function

The base R function, file.path(), allows us to construct the path to required files in a platform-independent way.

For example, if the file mydata.csv is in a sub-folder named datafiles, the syntax is:

file.path("datafiles", "mydata.csv")

The function returns the path using the correct separator for the OS on which the script containing the code is executed.

the here Package

When used in an R Project, the here package allows us to write the path to all files as relative paths from the main project folder.

It accomplishes this by looking for the .Rproj file relative to the current file’s location.

It needs to be installed to use the here package. If files are shared among different users or computers, they must be installed on all concerned computers.

Use the here package in each script or .Rmd document as follows:

  1. First, call here::i_am() with the path to the current file relative to the main project folder, which has the .Rproj file. Use the file.path() function to code this path.

  2. Load the here package with library(here).

  3. Construct relative paths to the required files from the main project folder using the here() function. The syntax is similar to file.path().

    However, here() uses its knowledge of the location of the .Rproj file to help R locate the referenced files.

Example:

# Suppose the folder structure of the project is as follows.

ProjFolder
|___Scripts
|      |__MyScript.R
|
|___Data
|      |__MyData.csv
|
|__Documents
       |__MyRMarkdown.Rmd


# In .R files. The same for .Rmd files.

# First, give the path of this script file relative to the main project folder.
here::i_am(file.path("Scripts", "MyScript.R"))

# Then, load the here package.
library(here)

# Subsequently, use the here() function whenever a path is written.
# Use a relative path from the main project folder.
myvar = read.csv2(here("Data", "MyData.csv"))

the Suggested Workflow

  1. Launch the project by opening the .Rproj file. This sets the working directory to the folder containing that file.
  2. Open the required files using the file panel in R Studio. This ensures that they get opened in this particular instance of R Studio with the correct working directory.
  3. In scripts and .Rmd files, follow the steps in the previous section to use the here package.

The whole project folder can reside at any location on the computer. It can be shared as a whole and used on any other computer which has the required software.

If the here() function conflicts with other packages, use the syntax here::here().

References

For details about R Studio Projects, see R for Data Science.

For details about the here package, first see the blog post: Why should I use the here package when I'm already using projects?. Then read the vignette of here.

Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.