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?
3 Answers
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") })

- 25,056
- 14
- 120
- 217
-
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
-
4This 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
-
3
-
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.

- 8,497
- 41
- 35
-
2I'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
-
2Here 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
Here's how I go about solving this problem. Lets say we have two Markdown files titled 'my_report_eng.rmd' and 'my_report_fr.rmd' as well as an output directory in /c/docs/reports/output/ as well as a location of these rmds in a source directory we will call /c/docs/reports/source. Goal is to run these two rmd files and output the results to our Output path. We can write a simple R script to achieve this.
source_folder <- file.path("C:","docs","reports","source")
output_folder <- file.path("C:","docs","reports","output")
timestamp <- Sys.Date()
#render english report
rmarkdown::render(input = paste0(source_folder, "/", "my_report_eng.rmd"),
output_format = "word_document",
output_file = paste0("report_en_", timestamp, ".docx"
output_dir = output_folder)
#render french report
rmarkdown::render(input = paste0(source_folder, "/", "my_report_fr.rmd"),
output_format = "word_document",
output_file = paste0("report_fr_", timestamp, ".docx"
output_dir = output_folder)
This method can be extended beyond two reports, or obviously scaled down to one.

- 89
- 11