I'm writing a knitr chunk hook and would like to check whether the user has explicitly set the results chunk option.
For "non-standard" chunk options, I can use knitr::opts_current$get("new_option") to determine whether or not the user explicitly provided the chunk option. If they did, opts_current$get() returns the value; if they did not, opts_current$get() returns NULL.
Unfortunately, the default value of the results chunk option is propagated to opts_current$get(), so this approach doesn't work. Instead it returns the default value:
As you want this behavior for options that have default values, you can't to that I think because options are read and merged with default one. So I think this would require something new in knitr to get the user provided option value. Would you open a feature request in GH repo to discuss this according to your use case ?
I've since discovered that knitr::knit_code$get("<chunk-label>") returns the un-evaluated chunk options as in the chunk_opts attribute. Using this approach lets you directly access the chunk options that were specifically applied to a given chunk. The downside is you may have to run it through eval(paste(text = chunk_option), envir = knitr::knit_global()) first.
Using this method, you can tell the difference between globally-set options and specifically set options. Inside a knitr hook, for example, you might use options$label to get the current chunk label's code a chunk opts to compare with the values in options.