How to use Rstudio relative paths

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 here().

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

Tags:

Path

R

Rstudio