Code chunks
Besides text, Quarto documents can also contain code. Unlike regular R or Python scripts, code in Quarto documents is run within so-called code chunks to separate text from code.
A code chunk looks as follows in a Quarto document:
## Methods
Here is some plain Markdown text,
for example describing your methods.
```{programming-language}
#| label: your-code-label
#| [other settings]
[your code here]
```
After the code chunk, you can write regular Markdown text again, like in a Discussion.
If you work with a visual editor, you can simply click Insert
> Code cell
> [your programming language]
to add a code chunk to your document.
What does a code chunk do?
A code chunk simply executes code that you specify. Depending on the content of your code, it may print code output into the document when the chunk is executed, or create new variables, for example.
If the default code chunk options are in place, rendering your Quarto file will involve each code chunk being run and the results being embedded beneath the code chunk in the final document.
Code Chunk Options
Code chunks can be customized via the code chunk yaml, denoted with #| chunk-option: setting-value
at the top of each code chunk.
The following code chunk options are useful to include:
#| label: your-code-chunk-title
: It is good practice to always label a code chunk. This is also handy when you want to reuse the chunk elsewhere (see Sourcing external code)#| eval: true
: Do you want to execute the code? If not, set eval tofalse
#| echo: false
: Do you want to show the source code in the finished document? If set tofalse
, the results of the computation will still be displayed in the document!#| output: true
: If you want to exclude the output of the computation into the resulting document, set output tofalse
.#| warning: false
: Choose whether to include warnings in the output documents. You usually want this to betrue
during the writing phase, butfalse
when rendering to the final document.#| include: false
: If set tofalse
, both code ánd computation results are suppressed from the final document. Note that the code is still run, and the results of the chunk can be used by other chunks.
If you have a code chunk that creates a figure, you can also set several options for the looks of the figure, such as:
#| fig.cap: "your figure's caption"
: caption of the figure#| fig-width: 8
: width of the figure#| fig-height: 6
: height of the figure
Chunk options for the entire document
In many cases, you want to use chunk options across all chunks in the document or in a project. In that case, you can put the chunk options that you want to apply across the document in the _quarto.yml
file or in the main yaml at the top of the document as such:
title: "My Document"
execute:
echo: false
warning: false
Exercises
Exercise 1
- Insert a setup code chunk t the beginning of your document (i.e. below the YAML header) to read in the necessary packages.
- Create a code chunk in the
Results
section of your document - Copy a piece/section of your script and paste it into the code chunk
- Run the code chunk and see if it works alright,
- Repeat the previous three steps until the whole dummy script has been incorporated within code chunks in your manuscript.
- Render the document and check the output.
Exercise 2
Play around with different code chunk options. Which chunk option would hide the code and only show results, as you might like in a final manuscript?