```{python}
1 + 1
```
2
This guide provides an overview of code execution options in Quarto, a versatile computational document system. Quarto supports multiple engines, including Jupyter and Knitr, and offers a range of options for customizing code outputs in your documents.
This guide covers the following topics:
Hide code from the output document using echo: false
in the document yaml
settings:
Override this option on a per code-block basis:
Code block options are included in a special comment at the top of the block. Lines at the top prefaced with #|
are considered options.
Options for customizing output:
Output Option | Description |
---|---|
eval |
Evaluate the code chunk (if false , just echos the code into the output). |
echo |
Include the source code in output. |
output |
Include the results of executing the code in the output (true , false , or asis to indicate that the output is raw markdown and should not have any of Quarto’s standard enclosing markdown). |
warning |
Include warnings in the 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 (e.g. include: false suppresses all output from the code block). |
Knitr example with some of these additional options included:
---
title: "Knitr Document"
execute:
echo: false
---
```{r}
#| warning: false
library(ggplot2)
ggplot(airquality, aes(Temp, Ozone)) +
geom_point() +
geom_smooth(method = "loess", se = FALSE)
```
```{r}
summary(airquality)
```
When using the Knitr engine, you can also use any of the available native options (e.g. collapse
, tidy
, comment
, etc.). See the Knitr options documentation for additional details. You can include these native options in option comment blocks as shown above, or on the same line as the {r}
as shown in the Knitr documentation.
Defaults figure width and height (expressed in inches per document type):
Document Format | Default Value |
---|---|
Default | 7 x 5 |
HTML Slides | 9.5 x 6.5 |
HTML Slides (reveal.js) | 9 x 5 |
5.5 x 3.5 | |
PDF Slides (Beamer) | 10 x 7 |
PowerPoint | 7.5 x 5.5 |
MS Word, ODT, RTF | 5 x 4 |
EPUB | 5 x 4 |
Hugo | 8 x 5 |
Change the default sizes using the fig-width
and fig-height
options:
---
title: "My Document"
format:
html:
fig-width: 8
fig-height: 6
pdf:
fig-width: 7
fig-height: 5
---
When using the Knitr engine, fig-width
and fig-height
are supported on a per-cell basis. But when using the Jupyter engine, these options only have an effect if specified at the document- or project-level metadata.
output: asis
for raw markdown output without Quarto’s standard divs.output: asis
.Output:
Note that we also include the echo: false
option to ensure that the code used to generate markdown isn’t included in the final output.
For the Jupyter engine, you can also create raw markdown output using the functions in IPython.display
:
If you are using the Knitr cell execution engine, you can specify default document-level Knitr chunk options in YAML. For example:
---
title: "My Document"
format: html
knitr:
opts_chunk:
collapse: true
comment: "#>"
R.options:
knitr.graphics.auto_pdf: true
---
You can additionally specify global Knitr options using opts_knit
.
The R.options
chunk option is a convenient way to define R options that are set temporarily via options()
before the code chunk execution, and immediately restored afterwards.
In the example above, we establish default Knitr chunk options for a single document. You can also add shared knitr
options to a project-wide _quarto.yml
file or a project-directory scoped _metadata.yml
file.
Keep intermediate files during rendering with these options:
Rendering Option | Description |
---|---|
keep-md |
Keep the markdown file generated by executing code. |
keep-ipynb |
Keep the notebook file generated from executing code (applicable only to markdown input files) |
For example, here we specify that we want to keep the jupyter intermediate file after rendering:
echo: fenced
to display code delimiters (e.g., ```{python}
) in tutorials or documentation.Output:
Useful for demonstrating cell options like output
and code-overflow
:
This behavior can also be specified at the document level if you want all of your executable code blocks to include the fenced delimiter and YAML options:
For unexecuted code blocks, use double curly braces around the language (e.g., {python}
):
If you want to show an example with multiple code blocks and other markdown, just enclose the entire example in 4 backticks (e.g. ````
) and use the two curly brace syntax for code blocks within. For example:
Usage of shell commands (from Bash, Zsh, etc.) within Quarto computational documents differs by engine.
Using Jupyter shell magics:
Note that !
preceding echo
is what enables a Python cell to be able to execute a shell command.
The Knitr engine also supports ```{python}
cells, enabling the combination of R, Python, and Bash in the same document