customizing r quarto pdf output file name
Asked Answered
T

3

5

How do I customize the filename of r quarto pdf document when I render? Before when I was using Rmarkdown I was using the following code in the YAML:

---
title: "Some title"
author: "First Last"
date: "`r format(Sys.Date(), '%d %B, %Y')`"
output: pdf_document
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = file.path(dirname(inputFile), paste0(Sys.Date(),"_Report","_FirstLast",".pdf"))) })
---

When I hit the "Knit" button the filename of the pdf document would be 2022-08-08_Report_FirstLast.pdf

Is there a way to do this with quarto pdf? I think the quarto_render function needs to be used but don't know how.

Timberhead answered 2/9, 2022 at 22:2 Comment(0)
A
12

Use the output_file argument of quarto_render function.

file_name = paste0(Sys.Date(),"_Report","_FirstLast", ".pdf")

quarto_render("your_qmd_file.qmd", output_file = file_name, output_format = "pdf")

Now about the approach you are trying,

  • Firstly, there's no such knit yaml key in quarto AFAIK

  • Secondly, although r-code can be used in code-chunk option prefixed be !expr , it's not possible to use inline R code in document yaml section right at this moment of answering this question (03 Sep, 2022). See this discussion on Github.

Though there are some suggested workaround for using r-code in yaml in this discussion on Github, but using quarto_render to control output filename seems the easiest option in your case.


And additionally, if your output file name is simple (that is, without any r-code syntax), you can use output-file yaml option.

---
title: "Testing Output file name"
format:
   pdf: 
    output-file: "output_file_name"
    output-ext:  "pdf"
---


## Quarto

Quarto enables you to weave together content 
and executable code into a finished document.
To learn more about Quarto 
see <https://quarto.org>.

## Running Code

When you click the **Render** button 
a document will be generated that 
includes both content and the output of 
embedded code. 

This will create the output file named output_file_name.pdf in the directory where the source file is.

Abukir answered 3/9, 2022 at 4:21 Comment(2)
Thank you. This works. Is there a way to add the Sys.Date() to the 'output-file' parameter?Timberhead
No its not possible to use Sys.Date in output-file right now.Abukir
C
4

If you're comfortable using Quarto CLI in bash shell (which may be more convenient for automatic report generation), you could date-stamp your outputs like this.

now=`date +"%Y-%m-%d"`
quarto render my_doc.qmd --output "./out_$now.pdf" --to pdf
Cupbearer answered 3/9, 2022 at 17:44 Comment(0)
K
1

CLI approach is useful for automation but not quite as convenient as hitting knit in RStudio when used to use RMarkdown.

I have used a different approach using quarto projects to achieve the same effect of date stamping output files.

Quarto has the ability to post process output files using scripts written in R/Python/Lua/Typescript. I only know R, so wrote a simple script to rename the output pdf files to include the current date.

It requires a _quarto.yml file to invoke a project and contains the following YAML to point to the post render script and direct the pdf to a specific directory (note the latex-output-dir parameter is specifically not indented):

project:
  post-render: ./scripts/datestamp_output_file.R
latex-output-dir: ./output

The datestamp_output_file.R script contains the following:

output_file <- Sys.getenv("QUARTO_PROJECT_OUTPUT_FILES")

file.rename(
  output_file,
  paste0(
    tools::file_path_sans_ext(output_file),
    format(Sys.time(), '-%Y-%m-%d'),
    '.',
    tools::file_ext(output_file))
)

When you hit render within RStudio, this post render script grabs the names of the files generated by quarto and then renames them with an appended current date. This current script will append date to all output files if there are more than one.

I know it's a bit hacky but it works OK if you are revising a document, say with updated data, and want to keep the pdf output but not so frequently that you want it to be automated.

There is one downside, you get an error saying no output was created by quarto render in the console and it won't display pdf in the viewer tab in RStudio like normal; it is expecting the original unaltered output file name. I am not sure this is fixable but if anyone knows how to fix it that would be great.

See https://quarto.org/docs/projects/scripts.html for further info on scripts

Kevenkeverian answered 19/10, 2023 at 19:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.