Patterns

A list of regular expressions to extract R code and chunk options from the input document

2017-02-03

The object knit_patterns manages patterns in knitr. For example, we can use knit_patterns$get() to check the current pattern list. A pattern list includes the following components:

Patterns that are NULL will not be matched.

Like Sweave, there are two types of R code in knitr: code chunks (like paragraphs) and inline R code which will be executed within text lines. For chunks in a document, options are of the form label, opt1=TRUE, opt2=FALSE, opt3='character.string' (connected by , and =; only the chunk label does not need to have a value, since it is implicitly the value of the chunk option label).

Built-in Patterns

There are several built-in pattern lists in knitr which are stored in all_patterns.

library(knitr)
str(all_patterns)

Knitr will first examine the content of the input to decide an appropriate set of patterns, if this automatic detection fails, then depending on the extension of the input filename, knitr will automatically choose a pattern list from the above lists, e.g. file.Rnw will use all_patterns$rnw, and file.html will use all_patterns$html, etc.

A series of convenience functions pat_rnw(), pat_html(), pat_md(), pat_tex() and pat_brew() can be used to set built-in patterns.