How-To: RStudio: Use Here Package for Platform Independent Relative Paths

Question

How do I make my RStudio project platform independent and utilize relative paths for portability?

 

Answer

1. Create a new project with renv.

Example project: my_research_project

Note:  To create a new project with renv, see How-To: RStudio: Create a New Project with Renv.

 

2. Install and load here package.

Note:  To install a package in Rstudio, see How-To: RStudio: Installing Packages.

 

3. Build and use platform independent paths.

Example project directory structure: 

my_research_project/

│── my_research_project.Rproj

│── data/

│   └── raw_data.csv

└── scripts/

    └── analysis.R

Example R code snippet: 

library(here) # to setup path

library(readr) # to read csv file

# Build the path dynamically

data_path <- here("data", "raw_data.csv")

# Read the data

my_data <- read_csv(data_path)

 

4. data_path is now relative to my_research_project and platform independent. my_data will be populated with contents of raw_data.csv regardless of operating system.

 

 

For Additional Assistance

For additional assistance or questions about this article, please contact itrss@missouri.edu