The knitr package uses a special object to control options and settings (denoted as obj below); it has the following methods:
obj$get(name): returns an option namednameor a list of several options ifnameis a character vector of length greater than 1, and it returns all the options ifnamenot providedobj$set(...): permanently changes options; the argument...can be of the formtag = valueor a list of optionslist(opt1 = value1, opt2 = value2)obj$merge(values): temporarily merges a list of new options into the current list and returns the merged list (original list not changed)obj$restore(): restores the object
These objects are visible to users in knitr:
opts_chunkandopts_current: manages options for code chunksopts_knit: manages options for the knitr packageknit_hooks: manages hook functionsknit_patterns: manages regular expressions to extract R code from the input documentknit_engines: functions to deal with other languages
Except knit_patterns, all other objects are initialized with default values, and knit_patterns will be automatically determined according to the type of input document if not provided. The knit_hooks object is supposed to be used most frequently, and the other three are usually not to be used directly. For example, opts_chunk is usually set in the input document rather than using the command line directly.
Knitr’s settings must be set in a chunk before any chunks which rely
on those settings to be active. It is recommended to create a knit
configuration chunk as the first chunk in a script with cache = FALSE and include = FALSE options set. This chunk must not contain
any commands which expect the settings in the configuration chunk to
be in effect at the time of execution. The configuration chunk could
look something like this:
<<setup, cache=FALSE, include=FALSE>>=
library(knitr)
opts_knit$set(upload.fun = imgur_upload, self.contained = FALSE,
root.dir = '~/R/project')
@
On a technical note, these objects are similar to closures – they
consist of a list of functions returned by a function. For details,
see the unexported function knitr:::new_defaults. The chunk options
are also managed by closures.
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:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. 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).