6

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()
    )
  })
Tom
  • 279
  • 1
  • 12

2 Answers2

12

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"
    )
  })
---
starja
  • 9,887
  • 1
  • 13
  • 28
2

Here is how you can specify both: the output directory, and add the current date to the name of your output file within YAML heading:

---
title: "test"
author: "myName"
date: "`r Sys.Date()`"
output: html_document
knit: (function(input, ...) {
    rmarkdown::render(
      input,
      output_dir = "../outReports",  
      output_file =file.path("../outReports",glue::glue('test_{Sys.Date()}'
    )))
  })
---
maycca
  • 3,848
  • 5
  • 36
  • 67