48

I am using RStudio's knit HTMl function to output some presentations. But it always outputs the files to my current work directory. How can I make it output to another directory so that my directory is clean with only the original .rmd files?

xiaodai
  • 13,509
  • 17
  • 70
  • 120

2 Answers2

38

The trick mentioned in Rmarkdown directing output file into a directory worked for me.

Example: Add the following to the YAML preamble as a top-level item to write output to the pdf/ subdirectory:

knit: (function(inputFile, encoding) {
  rmarkdown::render(inputFile, encoding = encoding, output_dir = "pdf") })
krlmlr
  • 23,618
  • 14
  • 112
  • 204
  • does this alter the working directory in any way? I just added a similar snippet to my preamble (specifiying the `output_file`), but somehow some objects are no longer found, when I knit the document via the knit button. When I comment out the new line in the preamble, everything works fine. – stats-hb May 14 '18 at 15:25
  • 1
    @stats-hb: I have no idea, but you can test it with a document that contains a chunk that shows `getwd()` . – krlmlr May 14 '18 at 21:09
  • 4
    This works for me and it does not alter the working directory. Thanks. – Shixiang Wang Jun 27 '19 at 08:31
  • @stats-hb it seems that it uses the Knitr default where the knit directory is where the .rmd is even if you have changed the knit directory using the dropdown beside the knit button. I added `knit_root_dir = rprojroot::find_rstudio_root_file()` to the `render` call and it worked to set the knit directory to the project directory – see24 Sep 24 '21 at 13:30
  • 2
    this doesn't work anymore – Our Feb 11 '22 at 10:13
18

As Eric pointed out in the comments, if you're willing to forego the convenience of the Knit HTML button (which produces HTML files that live alongside your .Rmd), you can just call rmarkdown::render directly.

However, if you really need to customize your workflow, you can override the Knit HTML button to run whatever command you via the rstudio.markdownToHTML option. This command could invoke rmarkdown with specific options (such as output directory) and perform other pre- or post-processing tasks. Documentation here:

https://support.rstudio.com/hc/en-us/articles/200552186-Customizing-Markdown-Rendering

Note that setting the rstudio.markdownToHTML option will turn off some of the newer RMarkdown V2 integration features baked into RStudio, since RStudio will no longer be able to infer what engine is being used to render the document.

Jonathan
  • 7,973
  • 41
  • 35
  • 2
    I'm still struggling with this (trying to have all intermediary/result files go to `/output/`, and I'd love to use the RStudio button. Could you provide a MWE? Unfortunately above rstudio docs link is paywalled or something. – maxheld Dec 15 '15 at 21:52
  • 3
    The link is dead now. – krlmlr Aug 30 '18 at 08:46
  • 1
    Here the archived page: https://web.archive.org/web/20150920182851/https://support.rstudio.com/hc/en-us/articles/200552186-Customizing-Markdown-Rendering – leogama Nov 09 '18 at 16:39