R Markdown - Variable Output Name

R Markdown - variable output name

You can keep the simplicity of using the RStudio Knit button and reproducibility of a YAML header by using the undocumented knit hook to redefine what the button does (default function called is rmarkdown::render). The output_file parameter of the render function specifies the file name, so by setting it you override the standard behaviour of using the same prefix as the input filename.

e.g. to always output a file called myfile.pdf

knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = file.path(dirname(inputFile), 'myfile.pdf')) })

The function can be an anonymous one-liner as well as imported from a package, as seen here with slidify.

You can set your own YAML headers (I don't know if this is generally advised anyway), accessible under rmarkdown::metadata$newheader but they don't seem available from within this sort of function as far as I can see.

As for passing file name in from an R chunk... if you're referring to code chunks below the YAML header, from my experience I don't think that's possible(?). Headers can contain inline R commands (single backtick-enclosed, starting with r), but seemingly not for this hook function.

Related:

  • Rmarkdown GitHub repo issue — output format-specific output_file
  • Blog post I wrote following this question [invalid link, domain for sale as of 20210216] / corresponding GitHub wiki notes

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

Insert date in filename while knitting document using RStudio Knit button

You can do this in the console:

library(knitr)  
knit("test.Rmd")
knit2html("test.md", output=paste0("test",Sys.Date(),".html")) # Sys.Date() is a string with the current date

Alternate, better version:

rmarkdown::render("test.Rmd",output_file=paste0('test',Sys.Date(),'.html'))

You can directly change the behavior of the RStudio knit button with some code in your document, like this.

To the header, before the output section add this code:

knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = paste0(substr(inputFile,1,nchar(inputFile)-4),Sys.Date(),'.html')) })

The substr(inputFile,1, nchar(inputFile)-4) strips the ".Rmd" from your Rmd filename.



Related Topics



Leave a reply



Submit