ABSTRACT

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. A chunk hook is a function stored in knit_hooks and triggered by a custom chunk option. A chunk hook can be arbitrarily named, as long as it does not clash with existing hooks in knit_hooks. This hook is used to set the margin parameter with par() for R base graphics. Since a chunk hook is a function, it also has a returned value. Chunk hooks that return character values allow us to write anything we want to the chunk output. With the hook hook_rgl(), we can easily save snapshots from the rgl package. The real hook function in knitr is much more complicated than this due to a lot of details to be taken into consideration.