Reputation: 16004
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?
Upvotes: 58
Views: 35589
Reputation: 89
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.
Upvotes: 0
Reputation: 25444
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") })
Upvotes: 54
Reputation: 8812
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.
Upvotes: 20