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..
dummy_function <- function() {
current_chunk <- knitr::knit_hooks$get('chunk')
knitr::knit_hooks$set(chunk = function(x, options) {
if (is.null(options$fig.alt)) {
options$fig.alt = "Hello World!"
} else {
return(current_chunk(x, options))
}
})
plot(pressure)
}
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 :
Another thing I've tried is:
dummy_function <- function() {
knitr::opts_hooks$set(fig.alt = function(options) {
if (options$fig.alt) {
options$fig.alt = "It worked!"
}
options
})
plot(pressure)
}
This sets the fig.alt to be TRUE, instead of It worked

