<commit_before>---
title: Hooks
subtitle: Customizable functions to run before / after a code chunk, tweak the output,
  and manipulate chunk options
date: '2017-02-03'
slug: hooks
show_toc: true
---

The object `knit_hooks` in the **knitr** package is used to set hooks; the basic usage is `knit_hooks$set(param = FUN)` (see [objects](../objects) for details) where `param` is the name of a chunk option (can be arbitrary), and `FUN` is a function. There are two types of hooks: chunk hooks and output hooks. Hook functions may have different forms, depending what they are designed to do.

## Chunk hooks

Chunk hooks are functions to be called before or after a code chunk when the chunk option is not `NULL` (it basically means as long as you set a option, the hook will be run), and they should be defined with three arguments:

```r 
foo_hook = function(before, options, envir) {
    if (before) {
        ## code to be run before a chunk
    } else {
        ## code to be run after a chunk
    }
}
```

When **knitr** is processing the document, `foo_hook(before = TRUE)` will be called before a code chunk is executed (unless the chunk is cached or set not to be evaluated), and `foo_hook(before = FALSE)` is called after a chunk; the argument `options` is a list of [options](options) in the current chunk (e.g. `options$label` is the label of the current chunk), and `envir` is the environment in which the code chunk is evaluated. The latter two arguments can be optionally used in a chunk hook. For example, if we set a hook for the `small_mar` option as:

```r 
knit_hooks$set(small_mar = function(before, options, envir) {
    if (before)
      par(mar = c(4, 4, .1, .1))  # smaller margin on top and right
})
```

Then this function will be called for a chunk like this:

````md
<!-- small_mar doesn't have to be TRUE; it can be any non-NULL value -->
```{r, myplot, small_mar=TRUE}
hist(rnorm(100), main = '')  # no main title<commit_msg>fix a broken link reported at https://yihui.org/404.html#comment-5208419821<commit_after>---
title: Hooks
subtitle: Customizable functions to run before / after a code chunk, tweak the output,
  and manipulate chunk options
date: '2017-02-03'
slug: hooks
show_toc: true
---

The object `knit_hooks` in the **knitr** package is used to set hooks; the basic usage is `knit_hooks$set(param = FUN)` (see [objects](../objects/) for details) where `param` is the name of a chunk option (can be arbitrary), and `FUN` is a function. There are two types of hooks: chunk hooks and output hooks. Hook functions may have different forms, depending what they are designed to do.

## Chunk hooks

Chunk hooks are functions to be called before or after a code chunk when the chunk option is not `NULL` (it basically means as long as you set a option, the hook will be run), and they should be defined with three arguments:

```r 
foo_hook = function(before, options, envir) {
    if (before) {
        ## code to be run before a chunk
    } else {
        ## code to be run after a chunk
    }
}
```

When **knitr** is processing the document, `foo_hook(before = TRUE)` will be called before a code chunk is executed (unless the chunk is cached or set not to be evaluated), and `foo_hook(before = FALSE)` is called after a chunk; the argument `options` is a list of [options](../options/) in the current chunk (e.g. `options$label` is the label of the current chunk), and `envir` is the environment in which the code chunk is evaluated. The latter two arguments can be optionally used in a chunk hook. For example, if we set a hook for the `small_mar` option as:

```r 
knit_hooks$set(small_mar = function(before, options, envir) {
    if (before)
      par(mar = c(4, 4, .1, .1))  # smaller margin on top and right
})
```

Then this function will be called for a chunk like this:

````md
<!-- small_mar doesn't have to be TRUE; it can be any non-NULL value -->
```{r, myplot, small_mar=TRUE}
hist(rnorm(100), main = '')  # no main title
{'fraction_non_alphanumeric': 0.09831531305003771, 'fraction_numerical': 0.010812169977369876, 'mean_word_length': 3.8276699029126213, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3789930', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1150, 'n_words': 603}