Code Cells: Knitr
Knitr is an R package for dynamic document generation. Learn more about using Knitr in the article on Using R.
Overview
Cell options affect the execution and output of executable code blocks. They are specified within comments at the top of a block. For example:
```{r}
#| label: fig-polar
#| echo: false
#| fig-cap: "A line plot on a polar axis"
```
Attributes
label |
Unique label for code cell. Used when other code needs to refer to the cell (e.g. for cross references |
classes |
Classes to apply to cell container |
Code Output
eval |
Evaluate code cells (if
|
echo |
Include cell source code in rendered output.
|
code-fold |
Collapse code into an HTML
|
code-summary |
Summary text to use for code blocks collapsed using |
code-overflow |
Choose how to handle code overflow, when code lines are too wide for their container. One of:
|
code-line-numbers |
Include line numbers in code block output ( For revealjs output only, you can also specify a string to highlight specific lines (and/or animate between sets of highlighted lines).
|
lst-label |
Unique label for code listing (used in cross references) |
lst-cap |
Caption for code listing |
tidy |
Whether to reformat R code. |
tidy-opts |
List of options to pass to |
collapse |
Collapse all the source and output blocks from one code chunk into a single block |
prompt |
Whether to add the prompt characters in R code. See |
class-source |
Class name(s) for source code blocks |
attr-source |
Attribute(s) for source code blocks |
Cell Output
output |
Include the results of executing the code in the output. Possible values:
|
warning |
Include warnings in rendered output. |
error |
Include errors in the output (note that this implies that errors executing code will not halt processing of the document). |
include |
Catch all for preventing any output (code or results) from being included in output. |
panel |
Panel type for cell output ( |
output-location |
Location of output relative to the code that generated it. The possible values are as follows:
Note that this option is supported only for the |
message |
Include messages in rendered output. Possible values are |
results |
How to display text results. Note that this option only applies to normal text output (not warnings, messages, or errors). The possible values are as follows:
|
comment |
Prefix to be added before each line of text output. By default, the text output is commented out by |
class-output |
Class name(s) for text/console output |
attr-output |
Attribute(s) for text/console output |
class-warning |
Class name(s) for warning output |
attr-warning |
Attribute(s) for warning output |
class-message |
Class name(s) for message output |
attr-message |
Attribute(s) for message output |
class-error |
Class name(s) for error output |
attr-error |
Attribute(s) for error output |
Figures
fig-width |
Default width for figures |
fig-height |
Default height for figures |
fig-cap |
Figure caption |
fig-subcap |
Figure subcaptions |
fig-link |
Hyperlink target for the figure |
fig-align |
Figure horizontal alignment ( |
fig-alt |
Alternative text to be used in the |
fig-env |
LaTeX environment for figure output |
fig-pos |
LaTeX figure position arrangement to be used in Computational figure output that is accompanied by the code that produced it is given a default value of If |
fig-scap |
A short caption (only used in LaTeX output). A short caption is inserted in |
fig-format |
Default output format for figures ( |
fig-dpi |
Default DPI for figures |
fig-asp |
The aspect ratio of the plot, i.e., the ratio of height/width. When |
out-width |
Width of the plot in the output document, which can be different from its physical |
out-height |
Height of the plot in the output document, which can be different from its physical |
fig-keep |
How plots in chunks should be kept. Possible values are as follows:
|
fig-show |
How to show/arrange the plots. Possible values are as follows:
|
out-extra |
Additional raw LaTeX or HTML options to be applied to figures |
external |
Externalize tikz graphics (pre-compile to PDF) |
sanitize |
sanitize tikz graphics (escape special LaTeX characters). |
interval |
Time interval (number of seconds) between animation frames. |
aniopts |
Extra options for animations; see the documentation of the LaTeX animate package. |
animation-hook |
Hook function to create animations in HTML output. The default hook ( Another hook function is |
Tables
tbl-cap |
Table caption |
tbl-subcap |
Table subcaptions |
tbl-colwidths |
Apply explicit table column widths for markdown grid tables and pipe tables that are more than Some formats (e.g. HTML) do an excellent job automatically sizing table columns and so don’t benefit much from column width specifications. Other formats (e.g. LaTeX) require table column sizes in order to correctly flow longer cell content (this is a major reason why tables > 72 columns wide are assigned explicit widths by Pandoc). This can be specified as:
|
html-table-processing |
If |
Panel Layout
layout |
2d-array of widths where the first dimension specifies columns and the second rows. For example, to layout the first two output blocks side-by-side on the top with the third block spanning the full width below, use Use negative values to create margin. For example, to create space between the output blocks in the top row of the previous example, use |
layout-ncol |
Layout output blocks into columns |
layout-nrow |
Layout output blocks into rows |
layout-align |
Horizontal alignment for layout content ( |
layout-valign |
Vertical alignment for layout content ( |
Page Columns
column |
Page column for output |
fig-column |
Page column for figure output |
tbl-column |
Page column for table output |
cap-location |
Where to place figure and table captions ( |
fig-cap-location |
Where to place figure captions ( |
tbl-cap-location |
Where to place table captions ( |
Cache
cache |
Whether to cache a code chunk. When evaluating code chunks for the second time, the cached chunks are skipped (unless they have been modified), but the objects created in these chunks are loaded from previously saved databases ( |
cache-vars |
Variable names to be saved in the cache database. By default, all variables created in the current chunks are identified and saved, but you may want to manually specify the variables to be saved, because the automatic detection of variables may not be robust, or you may want to save only a subset of variables. |
cache-globals |
Variables names that are not created from the current chunk. This option is mainly for |
cache-lazy |
Whether to |
cache-rebuild |
Force rebuild of cache for chunk |
cache-comments |
Prevent comment changes from invalidating the cache for a chunk |
dependson |
Explicitly specify cache dependencies for this chunk (one or more chunk labels) |
autodep |
Detect cache dependencies automatically via usage of global variables |
Include
child |
One or more paths of child documents to be knitted and input into the main document. |
file |
File containing code to execute for this chunk |
code |
String containing code to execute for this chunk |
purl |
Include chunk when extracting code with |