10

Hooks

Hooks are an important component to extend knitr. A hook is a user-defined R function to fulfill tasks beyond the default capability of knitr. There are two types of hooks: chunk hooks and output hooks. We have already introduced some built-in output hooks in Section 5.3, and how to customize both the chunk and inline R output. In this chapter we focus on chunk hooks.

10.1    Chunk Hooks

A chunk hook is a function stored in knit_hooks and triggered by a custom chunk option. All chunk hooks have three arguments: before, options, and envir (explained later).

10.1.1  Create Chunk Hooks

A chunk hook can be arbitrarily named, as long as it does not clash with existing hooks in knit_hooks. Names of all built-in hooks are:

For example, ...

Get Dynamic Documents with R and knitr, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.