You may want to use {here}
package which is really useful for working with filepaths within projects.
Suppose I have created a quarto project within Rstudio by following File -> New Project -> New Directory -> Quarto Project
and made some folders and files within it. Therefore my current project structure looks like
$
│ test_proj.Rproj
│ _quarto.yml
│
├─-data
│ mtcars.csv
│
└─-scripts
delete.qmd
Now to invoke the mtcars.csv
file from delete.qmd
you can do the following
---
title: "Test File"
format: html
---
## Quarto
```{r reading-data}
df <- read.csv(here::here("data/mtcars.csv"))
```
As per the documentation of here::here()
it will always locate the files relative to your project root
So thinking in terms of the project root (where the test_proj.Rproj
is), you write the path of mtcars.csv
in your qmd
or R-script
files as "data/mtcars.csv"
and It doesn't matter where the script files are, since you are invoking the csv files relative to the project root which is fixed within a project.
To know more about {here}
, see here