read - what are the advantages of relative paths in r
How to use Rstudio relative paths (2)
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()  "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:
~ represents the working environment. Unfortunately, it does not work.
The so-called here package is really useful for avoiding absolute paths in (as well as outside of) RStudio. Suppose you have an RStudio project and want to access the file
/data/file.txt. This would be done as follows. This way, you don't have to mess around with
getwd(), just work relative to your project root using
library(here) #> here() starts at C:/test/someproject here("data", "file.txt") #> "C:/test/someproject/data/file.txt" readLines(here("data", "file.txt")) #> "The here package is awesome!"
How here figures out where your project root is is described in
?here and also in the "Ode to the here package" by Jenny Bryan.
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") as pointed out by @Roman.