dirname(rstudioapi::getActiveDocumentContext()$path)
works for me but if you don't want to use rstudioapi and you are not in a proyect, you can use the symbol ~ in your path. The symbol ~ refers to the default RStudio working directory (at least on Windows).
If your RStudio working directory is "D:/Documents", setwd("~/proyect1")
is the same as setwd("D:/Documents/proyect1").
Once you set that, you can navigate to a subdirectory: read.csv("DATA/mydata.csv")
. Is the same as read.csv("D:/Documents/proyect1/DATA/mydata.csv")
.
If you want to navigate to a parent folder, you can use "../"
.
For example: read.csv("../olddata/DATA/mydata.csv")
which is the same as read.csv("D:/Documents/oldata/DATA/mydata.csv")
This is the best way for me to code scripts, no matter what computer you are using.