I'm trying to write a function that is used inside a Rmd code chunk. It should check if the code chunk it currently resides in has set fig.alt. If it does not, it should set the fig.alt to a specified value. If it is set, then leave as is.
Here is what I have so far after my prolonged struggles. I've been trying to follow the knitr Hooks page, to no avail..
When I run dummy_function() in a code chunk that doesn't set fig.alt, it just returns "Hello World" without running plot(pressure) nor setting the fig.alt :
Answer I gave there about modifying options in knitr
using options hook is often the first thing to try
---
title: test
output: html_document
---
```{r}
knitr::opts_hooks$set(engine = function(options) {
# do nothing for non R chunk
if (tolower(options$engine) != "r") return(options)
# if not fig.alt, then use a default one
if(is.null(options$fig.alt)) {
options$fig.alt = "individual slide notes"
}
options
})
```
```{r}
knitr::opts_current$get("fig.alt")
```