R: In RStudio how do I make knitr output to a different folder to avoid cluttering up my drive?
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.
Rmarkdown directing output file into a directory
You could try setting the out_dir
variable in the function you are giving knit
to render:
knit: (function(inputFile, encoding) {
out_dir <- 'test';
rmarkdown::render(inputFile,
encoding=encoding,
output_file=file.path(dirname(inputFile), out_dir, 'analysis.html')) })
In RStudio, where is knitr markdown output located?
Under certain conditions, RStudio moves the knitted (HTML or PDF) file to a temporary directory (located under tempdir()
). These conditions are:
- You are in a RStudio Project.
- The "Project build tools" are set to "Package".
- The RMD file is located in a subdirectory
vignettes
. - Knitting is initiated with the "Knit" button in RStudio.
Typically, this happens when knitting a package vignette and has the purpose of "keep[ing] your vignettes folder from becoming cluttered with HTML files".
Changing the Project build tools, changing the folder name or closing the project prevents this behavior. Another way to keep the generated (HTML or PDF) file is to call rmarkdown::render
interactively (although this is not exactly equivalent to the "Knit" button).
Note that this only affects where the generated file will be saved. The Working Directory while knitting is unaffected.
Is it possible to knit an R Markdown file only to a preview window/pane in RStudio without saving an html file?
I don't know of a way to attach this to the knit
button, but you could write a function that does this:
- get the current file open in the edit pane. This answer gives details on that.
- run
rmarkdown::render()
with theoutput_dir
argument set the way you want.
You can attach this function to a keyboard shortcut in RStudio using these instructions.
Here's a simple version of the function:
knit2 <- function(filename = rstudioapi::getSourceEditorContext()$path,
output_dir = tempdir()) {
result <- rmarkdown::render(filename, output_dir = output_dir)
getOption("viewer")(result)
}
Simply call knit2()
, and the output will be written to tempdir()
, which means the preview will appear in the Viewer pane. If you want it to write to "results"
, call it as
knit2(output_dir = "results")
If that's not a subdirectory of tempdir()
, the preview will appear in an external browser.
Save R output to a different directory
You have a file
argument in your write*
function. If your Output
directory is in your working directory, it works like this:
write.xlsx(df, file = "Output/table.xlsx")
write.csv(df, file = "Output/table.csv")
Dynamically naming the output file in a custom R-markdown function
If the fields that you want to use don't include R expressions, you can use yaml_front_matter()
to extract their values and use those to construct the name for the output file:
---
title: "Untitled"
author: "Jane Doe"
date: "18/02/2022"
output: word_document
knit: >
(function(input_file, encoding) {
metadata <- rmarkdown::yaml_front_matter(input_file)
output_file <- with(metadata, paste(title, "by", author))
rmarkdown::render(input = input_file, output_file = output_file)
})
---
204 No Content
If your fields do include R expressions, this becomes a little more involved. You can apply the same principle, but now instead of getting the front matter from the RMarkdown file, you get it from the intermediate Markdown file generated during the rendering process. Then rename the result.
That could look something like this:
---
title: "Untitled"
author: "Jane Doe"
date: "`r Sys.Date()`"
output: word_document
knit: >
(function(input_file, encoding) {
# Render, keeping intermediate files for extracting front matter
md_dir <- tempdir()
output_file_temp <- rmarkdown::render(
input = input_file,
output_file = tempfile(),
intermediates_dir = md_dir,
clean = FALSE
)
# Get the rendered front matter from the intermediate Markdown file
md_file <- fs::path_ext_set(fs::path_file(input_file), ".knit.md")
metadata <- rmarkdown::yaml_front_matter(fs::path(md_dir, md_file))
# Build the output file name based on rendered metadata
output_name <- with(metadata, paste(title, "by", author, "on", date))
# Add the file extension and move to the working directory
output_ext <- fs::path_ext(output_file_temp)
output_file <- fs::path_ext_set(output_name, output_ext)
fs::file_move(output_file_temp, output_file)
message("Output moved to: ", output_file)
})
---
204 No Content
Related Topics
Side by Side Histograms in the Same Graph in R
R Graphs: Creating Tufte's Horizontal Bar Lines
Add Download Buttons in Dt::Renderdatatable
Library/Package Development - Message When Loading
R Ggplot2 Add Today's Date to the Title
Running Multiple Linear Regressions Across Several Columns of a Data Frame in R
Scale_Color_Manual Colors Won't Change
Trying to Find Row Associated with Max Value in Dataframe R
Difference Between Mean(C(1,2,21)) and Mean(1,2,21)
Download Attachment from an Outlook Email Using R
Change the Color of Action Button in Shiny
How to Check If a Sequence of Numbers Is Monotonically Increasing (Or Decreasing)
R- Shiny Webserver on a Local Server
Clustering List for Hclust Function
How to Get Rstudio to Automatically Compile R Markdown Vignettes