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:
chunk.begin: the pattern for the beginning of a code chunk; it must contain a group defined by(), which will be used to extract chunk optionschunk.end: the pattern for the end of a chunk (the original meaning of this pattern in literate programming is different: it used to indicate the beginning of normal text; if you want the original meaning, see the package optionfilter.chunk.end)chunk.code: the pattern to be used to extract R code from a chunk by removing characters of this patterninline.code: the pattern to be used to extract the pieces of R code mixed inline with other texts (i.e. those which are not in separate code chunks); likechunk.begin, it must contain a groupinline.comment: the pattern of inline comments (tokens of inline R code will be removed from lines that match with this pattern)header.begin: the pattern to find out where the document header begins; this is used to insert some header information into the output document (e.g. commands in the preamble in LaTeX, or CSS styles in HTML)document.begin: the pattern to find out where the body of the document begins (it can be used, for example, to extract the LaTeX preamble so we can externalize tikz graphics, or insert code for syntax highlighting)
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.
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:[email protected] -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
[email protected]. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).