How can I adjust the output folder when using the knitr button in RMarkdown?

Viewed 267

If I have the following folder structure...

├── README.md 
│
├── project.RProj
│
├── data
│   ├── some_data.csv
│
│
├── notebooks
│   ├── analysis.Rmd
│
├── output

How do I change the yaml in my RMarkdown file to output the HTML file to the output folder instead of in the notebook folder when using the knit button?

I would like to be able to click knit in RMarkdown and end up with...

├── README.md 
│
├── project.RProj
│
├── data
│   ├── some_data.csv
│
│
├── notebooks
│   ├── analysis.Rmd
│
├── output
│   ├── analysis.html

I have seen that Yihui has a good example of how to edit outputs to add the date in the R Markdown cookbook (https://bookdown.org/yihui/rmarkdown-cookbook/custom-knit.html). Pasted below. But I would like to have an example for this specific use case.

knit: (function(input, ...) {
    rmarkdown::render(
      input,
      output_file = paste0(
        xfun::sans_ext(input), '-', Sys.Date(), '.html'
      ),
      envir = globalenv()
    )
  })
1 Answers

You can specify the output_dir argument, in your case a basic YAML header looks like this:

---
title: "Test"
output: html_document
knit: (function(input, ...) {
    rmarkdown::render(
      input,
      output_dir = "../output"
    )
  })
---
Related