11

I am currently trying to transition to Quarto from Rmarkdown. However, I am facing an issue with setting the correct project directory. I wanted to ask if there is something similar to this option in Rmarkdown with Quarto. enter image description here

My understanding is that I need to make a .yml file to specify the directory I want to use, which is not very straightforward for me.

5 Answers5

7

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

shafee
  • 15,566
  • 3
  • 19
  • 47
4

You can use the output-file: ex: output-file: Projects/index.qmd this will save your current file to the directory, but a basic level of Parent/child and absolute/relative paths would be required.

Currently this is an empty directory.

enter image description here

Then a simple example is:

---
output-file: test/index.qmd
---

Which then renders the file to the specified directory

enter image description here

Keep in mind my delete.qmd is saved to my Desktop, then I have it output another file in /test directory and have Quarto save the output as another file name. this doesn't need another YAML file, we use inline YAML

Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
  • 1
    This is helpful, thanks! My problem actually is not with outputting, it's about invoking or calling files. For instance, I want to save `delete.qmd` inside a folder called `scripts` and this `scripts` folder is a part of my R project. Suppose I am working on `delete.qmd`, and I want to call a `something.csv` file located in a folder called `data`, which is also in my project directory. I would like to do so by writing `read_csv("data/something.csv")`. From what I see, this is not possible unless the `data` folder is inside the same folder the Quarto file is stored in. – Abdullah Abdelaziz Aug 01 '22 at 20:15
2

You can add global knitr options in the document yaml:

---
title: "My Document"
format: html
knitr:
  opts_knit:
    root.dir: "c:/my/project/directory"
---
Mike O'Brien
  • 136
  • 5
  • 2
    I tried this but it didn't work. Still had to use `knitr::opts_knit$set(root.dir = '/path/to/dir')` from within a chunk... Any ideas? – Dijkie85 Sep 05 '22 at 19:55
0

Another option is to use:

quarto::quarto_render(here::here("path_to/doc.qmd"),
                      execute_dir = here::here(),
                      output_format = "all", as_job = TRUE)

I am using this to get output in multiple formats and found that setting execute_dir made the file paths work

see24
  • 1,097
  • 10
  • 21
0

A little bit late, but I solved this as follows:

  1. Create a Quarto Project directly form RStudio (go to File : New Project… command, specify New Directory, then choose Quarto Project).
  2. When your Quarto Project is created, it will have a "_quarto" YAML file.
  3. Open it with a text editor and add the next line just below title: output-dir: output
  4. Create a new directory called "output" inside your Quarto Project.
  5. Your files will be rendered in the new "output" directory!
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 31 '23 at 06:12