Open rubenv95 opened 6 months ago
Example:
---
title: "Captioner"
author: "Ruben Vine"
date: "2024-05-16"
output: html_document
---
```{r Captioning}
# Resetting number to 0 for figure numbering
if(exists("figure_n")) {
rm(figure_n)
}
# Below functions assign a number and name to a figure, so the numbering is in order throughout.
figure_numbering = function(figure_name, caption) {
# Assigning n to 0 if it doesn't exist in global environment
if(!exists("figure_n")) {
figure_n <<- 0
}
# Adding 1 to n each time function is run
figure_n <<- figure_n+1
# Create title for figure using caption and numbering
title_temp <<- paste0("Figure ", figure_n, ": ", caption)
# Assign the global environment variable a name using the input figure_name
assign(paste0("title_", figure_name), title_temp, envir = .GlobalEnv)
}
figure_title = function(figure_name) {
# Pulling title from the global environment created in figure_numbering function
get(paste0("title_", figure_name), envir = .GlobalEnv)
}
# Just getting "Figure x" from title to refer to in the text
figure_text = function(figure_name) {
# Pulling title from the global environment created in figure_numbering function
title = get(paste0("title_", figure_name), envir = .GlobalEnv)
text = stringr::str_split(title, ":")[[1]]
text[1]
}
figure_numbering("overview_chart",
"Overview of the number of COVID-19 Cases")
figure_numbering("timeseries_chart",
"Timeseries of the number of COVID-19 Cases, October 2020 to September 2021")
As r figure_text("overview_chart")
shows below, ...
r figure_title("overview_chart")
# Insert overview chart code
As r figure_text("timeseries_chart")
shows below, ...
r figure_title("timeseries_chart")
There may be other ways to deal with table/figure numbering in officedown: https://ardata-fr.github.io/officeverse/officedown-for-word.html
Consider adding a figure numbering/captioner type function series, to get the script to automatically generate figure numbering, based on a figure ID. By using IDs, you can ensure the figure numbers are in the right order, and it saves you recalculating all later numbers after adding in a new figure, for example.
Some work already done outside of a package, emailed to Alan and Rosalyn. It needs some work to get it ready for the package.