Sosel
Sosel

Reputation: 1728

R Markdown - variable output name

With one R markdown file, I would like to create different possible output pdf documents, where the output file name should be defined within the document. Is there any way to convince markdown to manipulate the output filename in such a way? Ideally I would like to pass the filename by an r chunk.

Upvotes: 60

Views: 45491

Answers (4)

Raymond
Raymond

Reputation: 117

My current solution for this and similar questions is through 2 scripts:

  1. Script1: "xxx.md" file with flexible yaml header, similar to Floris Padt's. This header allows you to generate flexible pdf files with specified title, dates, and other features if you change the params. However, it could not specify flexible pdf names when you render it.
---
params: 
    feature_input: "XXXA"
    date: "08/18/2022"
title: "`Test For `r params$feature_input``"
author: "You Name"
date: "`r params$date`"
output: 
  pdf_document:

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown and data process

```
#Get parameter from yaml head
input_para <- params$feature_input
input_para
```

  1. Script2: "YYY.R", which specify params in xxx.md file, and specify pdf file names by output_file when rendering.
featureNames <- c("aaa", "bbb", "ccc")
setwd("path to xxx.md")
for (currentFeature in featureNames) {
  rmarkdown::render("xxx.Rmd", 
                    params = list(feature_input = currentFeature,
                                  date = Sys.Date()),
                    output_file=paste0("output/",currentFeature))
}
  1. You could update featureNames in yyy.R file, and run yyy.R to get the most flexible use of your xxx.md file.

This solution allows you to:

  1. update yaml header parameters,
  2. apply updated yaml parameters in your .md code chunk,
  3. and save your pdf with specific and flexible names.

Upvotes: 0

Floris Padt
Floris Padt

Reputation: 906

I played around with the Knitr-hook without fully understanding how it works and ran into an ugly workaround. The below coding seems to do the trick. Would be nice if somebody can either explain why it works and/or if it can written less ugly.

For now I lost the shiny input screen but believe this can even be added later. The good thing is that the R-Studio Knit button can still be used.

Please note that the subtitle and the file name are both: This Works! even with space and exclamation mark. The file is saved as This Works!.pdf

The filename and subtitle are set by assigning the text to the object pSubTitle. Note that the params are still in the YAML but are not resulting in a shiny popup screen as they are assigned in the Knitr-hook

enter image description here

---
params: 
  sub_title:
    input: text
    label: Sub Title
    value: 'my_Sub_Title_and_File_Name'
title    : "Parameterized_Title_and_output_file"
subtitle : "`r params$sub_title`"
output:
  pdf_document:
    keep_tex: false
knit: (
  function(inputFile, encoding) { 
  
    pSubTitle <- 'This Works!'
  
    rmarkdown::render( 
      input       = inputFile, 
      encoding    = encoding, 
      params      = list(sub_title = pSubTitle),      
      output_file = pSubTitle) })
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. ....

Upvotes: 19

Louis Maddox
Louis Maddox

Reputation: 5576

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:

Upvotes: 67

ilya
ilya

Reputation: 3164

This is pretty much what I do:

rmarkdown::render('my_markdown_report.Rmd',
                  output_file = paste('report.', Sys.Date(), 
                                      '.pdf', sep=''))

I have three scripts - one pulls the data and process it, second created charts & tables for report. Third one creates report based on markdown file. Code you see above is the part of the third script

Upvotes: 35

Related Questions