5

Is there a YAML option, which sets knit directory for an Rmd file? I.e. does the same as illustrated in Fig.1. The problem is that every time I copy files or share a project with other people (which usually have different default RStudio options than I do), the knit directory should be reset manually in each file. I'm aware of the global option (Fig.2), but I need a more reproducible solution which works for individual Rmd files in both both RStudio notebook and knitting modes. And, for instance, I know that there is an option such as:

editor_options: 
  chunk_output_type: console

Any ideas?

enter image description here

Fig. 1 Setting knit directory via menu commands.

enter image description here

Fig. 2 Global option to set default knit directory in RStudio.

GegznaV
  • 4,938
  • 4
  • 23
  • 43

1 Answers1

9

You could try to specify the knit field in the front-matter of our .Rmd file. Example .Rmd that would knit into "some_dir" created in your home directory:

---
title: "Untitled"
output: html_document
knit: (function(inputFile, encoding) {
    rmarkdown::render(inputFile, encoding = encoding, output_dir = "~/some_dir/")
  })
---

# Some content

Some content
Jozef
  • 2,617
  • 14
  • 19
  • I was not aware of this functionality, which changes the output directory. It is useful. Yet it is not the same, as "knit directory", which also changes the working directory. @Jozef Could you provide a link where I could read more about YAML option `knit`? – GegznaV Aug 14 '19 at 04:11
  • `rmarkdown::render()` has more interesting options worth exploring. Thus, I accept your answer. – GegznaV Aug 14 '19 at 21:00
  • but how do you specifically refer to the `Project` directory, as the question suggests? Thanks! – Matifou Aug 22 '20 at 23:50
  • 1
    @Matifou, if you really want to refer to the current RStudio project directory you can use `output_dir = rstudioapi::getActiveProject()`, note that this will likely only work in an RStudio context though. – Jozef Aug 24 '20 at 09:20
  • 1
    great, thanks @Jozef !! And nice blog by the way :-) – Matifou Aug 24 '20 at 15:49
  • Magnificent! This has been very useful! – Moses Apr 06 '21 at 11:32