How to use Rstudio relative paths

Worice picture Worice · Apr 25, 2016 · Viewed 53.5k times · Source

How can I use relative paths in a RStudio project environment?

For example, to access a file, I use the whole path:

# My RStudio project working directory:

getwd()
[1] "C:/Users/MaurizioLocale/OneDrive/Data_Science/10_Capstone_project/
     CP_Natural_Language/MY_FILE.txt"

But it is really long.

I am trying to use paths relative to the working environment. I tried something conceptually similar to:

"~/MY_FILE.txt"

where ~ represents the working environment. Unfortunately, it does not work.

Answer

user3507584 picture user3507584 · Nov 1, 2016

You could change the working directory. Get the address in the beginning getwd(), replace it by your project folder with setwd(). Then, when accessing a file just use read.table("./folder/file.R").