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
- We get a new instance of R Studio with a workspace dedicated to that project.
- The folder in which the
.Rprojexists is made the working directory.
.Rmd file in a sub-folder of this project folder will temporarily change the working directory to its folder when it is
here package mentioned below solves this problem.
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:
The function returns the path using the correct separator for the OS on which the script containing the code is executed.
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.
here package in each script or
.Rmd document as follows:
here::i_am()with the path to the current file relative to the main project folder, which has the
.Rprojfile. Use the
file.path()function to code this path.
Construct relative paths to the required files from the main project folder using the
here()function. The syntax is similar to
here()uses its knowledge of the location of the
.Rprojfile to help R locate the referenced files.
# 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
- Launch the project by opening the
.Rprojfile. This sets the working directory to the folder containing that file.
- 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.
- In scripts and
.Rmdfiles, follow the steps in the previous section to use the
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.
here() function conflicts with other packages, use the syntax
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