Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: In RStudio how do I make knitr output to a different folder to avoid cluttering up my drive?

Tags:

r

rstudio

knitr

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?

like image 484
xiaodai Avatar asked Oct 03 '14 07:10

xiaodai


People also ask

How do I export RMD from RStudio?

In R StudioOpen the R Markdown file in RStudio, and then select the Export to RCloud notebook item from the Addins menu. This will open a new tab or window in your default browser, with a form. Select or type in the URL of your RCloud installation, and click on Export.

Where does R Markdown save to?

rmarkdown will use the formatting instructions that you provided with markdown syntax. Once the file is rendered, RStudio will show you a preview of the new output and save the output file in your working directory.

How do I import RMD into RStudio?

To open a new file, click File > New File > R Markdown in the RStudio menu bar. A window will pop up that helps you build the YAML frontmatter for the . Rmd file. Use the radio buttons to select the specific type of output that you wish to build.

What is the knitr package in R?

The R package knitr is a general-purpose literate programming engine, with lightweight API's designed to give users full control of the output without heavy coding work. It combines many features into one package with slight tweaks motivated from my everyday use of Sweave.


2 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") }) 
like image 73
krlmlr Avatar answered Sep 29 '22 07:09

krlmlr


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.

like image 44
Jonathan Avatar answered Sep 29 '22 09:09

Jonathan