11.18 Option hooks (*)

Sometimes you may want to change certain chunk options dynamically according to the values of other chunk options. You may use the object opts_hooks to set up an option hook to do it. An option hook is a function associated with the option and to be executed when a corresponding chunk option is not NULL. This function takes the list of options for the current chunk as the input argument, and should return the (potentially modified) list. For example, we can tweak the fig.width option so that it is always no smaller than fig.height:

  1. knitr::opts_hooks$set(fig.width = function(options) {
  2. if (options$fig.width < options$fig.height) {
  3. options$fig.width <- options$fig.height
  4. }
  5. options
  6. })

Because fig.width will never be NULL, this hook function is always executed before a code chunk to update its chunk options. For the code chunk below, the actual value of fig.width will be 6 instead of the initial 5 if the above option hook has been set up:

  1. ```{r fig.width = 5, fig.height = 6}
  2. plot(1:10)
  3. ```

As another example, we rewrite the last example in Section 11.12 so we can use a single chunk option console = TRUE to imply comment = "" and prompt = TRUE. Note that console is not a built-in knitr chunk option but a custom and arbitrary option name instead. Its default value will be NULL. Below is a full example:

  1. ```{r, include=FALSE}
  2. knitr::opts_hooks$set(console = function(options) {
  3. if (isTRUE(options$console)) {
  4. options$comment <- ''; options$prompt <- TRUE
  5. }
  6. options
  7. })
  8. ```
  9. Default output:
  10. ```{r}
  11. 1 + 1
  12. if (TRUE) {
  13. 2 + 2
  14. }
  15. ```
  16. Output with `console = TRUE`:
  17. ```{r, console=TRUE}
  18. 1 + 1
  19. if (TRUE) {
  20. 2 + 2
  21. }
  22. ```

The third example is about how to automatically add line numbers to any output blocks, including source code blocks, text output, messages, warnings, and errors. We have mentioned in Section 5.7 how to use chunk options such as attr.source and attr.output to add line numbers. Here we want to use a single chunk option (numberLines in this example) to control the blocks to which we want to add line numbers.

  1. knitr::opts_hooks$set(
  2. numberLines = function(options) {
  3. attrs <- paste0("attr.", options$numberLines)
  4. options[attrs] <- lapply(options[attrs], c, ".numberLines")
  5. options
  6. }
  7. )
  8. knitr::opts_chunk$set(
  9. numberLines = c(
  10. "source", "output", "message", "warning", "error"
  11. )
  12. )

Basically, the option hook numberLines appends the attribute .numberLines to output blocks, and the chunk option numberLines set via opts_chunk$set() makes sure that the option hook will be executed.

With the above setup, you can use the chunk option numberLines on a code chunk to decide which of its output blocks will have line numbers, e.g., numberLines = c('source', 'output'). Specifying numberLines = NULL removes line numbers completely.

You may wonder how this approach differs from setting the chunk options directly, e.g., just knitr::opts_chunk$set(attr.source = '.numberLines') like we did in Section 5.7. The advantage of using the option hooks here is that they only append the attribute .numberLines to chunk options, which means they will not override existing chunk option values, e.g., the source code block of the chunk below will be numbered (with the above setup), and the numbers start from the second line:

  1. ```{r, attr.source='startFrom="2"'}
  2. # this comment line will not be numbered
  3. 1 + 1
  4. ```

It is equivalent to:

  1. ```{r, attr.source=c('startFrom="2"', '.numberLines'}
  2. # this comment line will not be numbered
  3. 1 + 1
  4. ```