R Markdown - variable output name

32,372

Solution 1

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:

Solution 2

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

Solution 3

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. ....
Share:
32,372

Related videos on Youtube

Sosel
Author by

Sosel

Good news, everybody!

Updated on April 08, 2022

Comments

  • Sosel
    Sosel about 2 years

    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.

    • rmuc8
      rmuc8 over 9 years
      could you post your command? do you want to name files "dynamically"? paste0("file_",x,".pdf")? x could be a date or the name of a dataset
    • Sosel
      Sosel over 9 years
      So far I was using RStudio, performing the knit command without actually looking into more details. But I should have a closer look into ilyas answer, sounds like a much clearer approach.
  • Sosel
    Sosel over 9 years
    So far I was only using RStudios knit command to compile some markdown file. If I understand you correctly, you create the actual markdown file using R commands, and even steer the knitting of the markdown using R commands? I was not aware of this approach so far, but I will have a closer look. So far I was also preparing the data and process it in a first script, but already at the second step I manually created a markdown file, putting e.g. various data sources into some report.
  • ilya
    ilya over 9 years
    Yes. markdown file is separate script on its own, it reads data places charts & tables together. It is called however from the outside script - you can also start/execute it from Rstudio and and will create file with the same name. I however call it from separate script and after rmarkdown creates pdf resulting file is copied into different places
  • jzadra
    jzadra almost 7 years
    Error in rmarkdown::metadata$title <- "My Title" : object 'rmarkdown' not found
  • StasK
    StasK over 6 years
    @yihuixie, can I pass params$whatever into the output file names that way? My markdown uses params$data to pick up the relevant data file, but it does not look like this works within your one-liner -- params$data not found. I can pass whatever I want from a script with rmarkdown::render(params=list(data="Oct2017data"),output_fil‌​e="Oct2017_analysis.‌​html"), but obviously I want to be able to only type it once.
  • zoowalk
    zoowalk almost 4 years
    @louis-maddox "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(?). " Is this still the case? Many thanks!
  • MJimitater
    MJimitater over 2 years
    @zoowalk same question. Did you find anything btw?