How to use relative paths in R
This guide shows you how to write an R script whose file paths keep working when you move it between your own computer and LINCOMM (Linux Community Servers). R is the trickiest of the statistics packages for this, because it has no built-in notion of "the folder this script is in." You set the anchor yourself. For the concept behind this, see Understanding relative paths.
Prerequisites
- An R script and its data files, kept together in one project folder.
- You run scripts with
Rscript(the usual way on LINCOMM).
Steps
-
Lay out your project so the data sits in a known place relative to the script:
myproject/ analysis.R data/ transactions.csv -
At the top of
analysis.R, work out the script's own folder and use it as the anchor. When you run a script withRscript, R records the script's location, and these lines read it back:args <- commandArgs(trailingOnly = FALSE) script_path <- sub("^--file=", "", args[grepl("^--file=", args)]) here <- dirname(normalizePath(script_path))The variable
herenow holds the full path to the folder containinganalysis.R, whatever computer you're on. -
Build every data path from
hereusingfile.path(), then read as normal:data_file <- file.path(here, "data", "transactions.csv") df <- read.csv(data_file)To reach a file in the parent folder, anchor to
dirname(here), which is one level up. -
Run the script:
Rscript analysis.R
If you work in an interactive R session
When you type commands directly into R rather than running a script file, there is no script location to read. Set the anchor yourself by changing the working directory to your project folder, then use plain relative paths:
setwd("/mnt/aae/users/jdoe/myproject")
df <- read.csv("data/transactions.csv")This is the one place you name an absolute path, and it's the one line you'd change when moving between machines.
Verify it worked
Copy the whole myproject folder to LINCOMM, then run the script from a different folder than the project itself:
cd /mnt/aae/users/jdoe
Rscript myproject/analysis.RIt should find transactions.csv and run without any path edits, even though you launched it from outside the project folder.
If something went wrong
- "cannot open file" or "No such file or directory": Check that the data folder was copied alongside the script, and that the names in
file.path()match the real folder and file names exactly (Linux is case-sensitive). herecomes out empty or wrong: This method needsRscript. If you're pasting code into an interactive session, use thesetwd()method above instead.