knitr 1.32
NEW FEATURES
-
The chunk option
ref.label
can be used to reuse code from other code chunks in the current code chunk. Now it can also accept a character vector of chunk labels wrapped inI()
so that the chunk options of other code chunks can also be applied to the current chunk. For example, if we have a chunk```{r, chunk-a, echo=FALSE, results='asis'}
, the chunk```{r, chunk-b, ref.label=I('chunk-a')}
will inherit both the code and chunk options fromchunk-a
(e.g.,echo=FALSE
andresults='asis'
), butchunk-b
can still override these chunk options, e.g., the actual chunk options of```{r, chunk-b, echo=TRUE}
will beecho=TRUE
andresults='asis'
. If you want to use one code chunk with several chunk options for multiple times, and each time you want to change a small subset of the chunk options, you may use this approachref.label = I()
to avoid typing most chunk options over and over again (thanks, @atusy, #1960). -
The chunk option
opts.label
gained a new usage. It used to mean labels inknitr::opts_template
(see the help page if you are not familiar with this object). Now it can also be used to specify the labels of other code chunks, from which the chunk options are to be inherited to the current chunk. For example,```{r, chunk-b, opts.label='chunk-a'}
means thatchunk-b
will inherit chunk options fromchunk-a
. Local chunk options in a chunk (if exist) will override inherited options. For the new features about both chunk optionsref.label
andopts.label
, see the detailed documentation and examples at https://yihui.org/knitr/options/ (thanks, @atusy, #1960). -
The internal code to process R code chunks was factored out into the language engine
R
, which can be obtained viaknit_engines$get('R')
now (thanks, @abhsarma, #1963). -
Added arguments
dir
andenvir
toload_cache()
to specify the working directory and environment into which the cached objects are loaded. By default, all cached objects are loaded into the global environment, which may not be desirable (thanks, @LTLA, #1905). -
The internal function
html_screenshot()
, which takes screenshots of HTML widgets and Shiny apps in knitr documents, now prefers the webshot2 package over the webshot package. This is because the backend of the webshot package is PhantomJS, which has been archived since 2018. If webshot is still preferred, use the chunk optionopts_chunk$set(webshot = "webshot")
(thanks, @atusy #1918, @LouisStAmour #1858). -
Added a new engine,
bslib
, which is a specialscss
/sass
engine for writing Sass code that wants to leverage Bootstrap Sass. It's primarily designed to work withrmarkdown::html_document_base
'stheme
parameter (when it represents abslib::bs_theme()
), but the engine can be used anywhere abslib::bs_theme()
is set globally withbslib::bs_global_set()
(thanks, @cpsievert, #1666). -
When the chunk option
include = FALSE
,error = TRUE
used to be ignored, i.e., errors are signaled nonetheless (causing the knitting process to fail). To avoid this problem, you can now use the numeric valueerror = 0
so that knitr will not check theinclude
option, i.e., knitting will not fail even ifinclude = FALSE
and an error has occurred in a code chunk, which you wouldn't notice (thanks, @rundel, #1914). -
When processing package vignettes that require rmarkdown or markdown, we now check that it is declared as a package dependency in the
DESCRIPTION
file (thanks, @dmurdoch, #1919). -
For
tikz
code chunks, a new optionengine.opts$extra.preamble
allows arbitrary LaTeX code to be inserted into the preamble of the template. This allows loading of additional LaTeX and tikz libraries without having to recreate a template from scratch (thanks, @krivit, #1886).
BUG FIXES
-
Fixed an issue where code source and results would not show when using a numeric vector in
fig.keep
to select all plots (thanks, @dongzhuoer @ajrgodfrey #1579, @cderv #1955). -
Graphical parameter
par(col =)
is now preserved as expected in following chunks whenknitr::opts_knit$set(global.par = TRUE)
(thanks, @arnonl, @RolandASc, #1603). -
Fixed an issue with
kable(format = "latex", booktabs = TRUE)
when the first cell of the table stats with[
(thanks, @jonnypenarth, #1595). -
For child documents,
include_graphics(error = FALSE)
is the default now (#1957). -
For Rnw documents, the commented
%\begin{document}
will no longer cause trouble (thanks, @NewbieKnitter @shrektan, #1819). -
Fixed an issue with the chunk option
fig.alt
causing figures to disappear inrmarkdown::word_document()
output. This option is currently supported for HTML output only. If provided for office output in rmarkdown, it will emit a warning and be ignored (#1966). -
Spaces in messages were not properly escaped in
.Rnw
documents (thanks, @elbersb, #1978). -
The hook
hook_pdfcrop()
did not crop figures generated by thetikz
ordot
engine (thanks, @giabaio yihui/xaringan#285, @cderv #1923).
MINOR CHANGES
-
For Rnw documents, if a chunk's output ends with
\n
, knitr will no longer add another\n
to it (thanks, @krivit #1958, @jpritikin 1092). -
For
kable(x, format = 'simple')
, it no longer generates apipe
table but asimple
table instead whenx
has only one column (thanks, @alusiani, #1968). Forkable(x, format = 'pipe')
, it no longer warns whenx
has no column names, but just generate an empty table header. -
For
tikz
code chunks, the default LaTeX template uses\documentclass[tikz]{standalone}
instead of\documentclass{article}
and\usepackage{preview}
now (thanks, @XiangyunHuang, #1985).