Is there a way to include automatic incremental document versioning when working in markdown/rmarkdown/quarto? When I first render a file to pdf I want it to display version 1.0 and increment upwards from there every time the pdf is updated. Is this something that’s possible?
I tried including a custom function based on the presence of a cache file but it didn’t work. It seems that the YAML header option "version" is not accepted (I really convinced myself it did).
Here’s my YAML header:
—
title: "Project: Example"
date: "April 2024"
documentclass: report
version: 1.0
—
This is my function:
cache_dir <- "./cache"
# Function to increment and return version number
version_number <- function() {
if (!file.exists(file.path(cache_dir, ".RData"))) {
version <- 1.0
save(version, file = file.path(cache_dir, ".RData"))
} else {
load(file = file.path(cache_dir, ".RData"))
version <- get("version") + 1.0
save(version, file = file.path(cache_dir, ".RData"))
}
return(paste0("v", version))
}
This also does not work:
—
title: "Project: Example"
date: "April 2024"
documentclass: report
version: 1.0
—
Does anyone know how I can implement such behavior? I’m not looking for version control like Git, and I’m using Visual Studio Code, if that helps. Thanks!
2
Answers
One thing you could do is store the version number as a parameter in your R Markdown file and then use an external R Script to render the R Markdown file.
Contents of R Markdown
Contents of R Script
After editing the R Script to point to the desired R Markdown File, you could run it. The script will update the version number and then render the R Markdown file.
Output
If you were using RStudio, you could store the version number as a parameter in your R Markdown file. You could then also include a rendering chunk in the R Markdown file. Running the rendering chunk would update the version and render the file.
R Markdown File
Output