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/someprojecthere("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.