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