rpathrstudio

How to use Rstudio relative paths


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.


Solution

  • 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").