Code Chunks in Quarto

Learn Quarto code chunks interactively: run R inside your document and control output with chunk options like echo, eval, warning and figure sizing. Live, runnable examples.

The whole point of Quarto is that your document can run code. A code chunk is a block of R fenced with ```{r}; when you render, Quarto executes it and drops the result into the page. Below, the code boxes are live — edit them and press Run to see it happen. (The first run loads R into your browser, which takes a few seconds.)

Your first chunk

This chunk creates some data and prints its mean. Change the numbers and run it again:

Whatever the code prints becomes part of the document. That’s how a report’s numbers stay in sync with its data — there’s no copy-pasting.

Chunks can draw

If a chunk produces a plot, Quarto embeds the figure. Run this:

Controlling a chunk with options

You often want to tweak how a chunk behaves — hide the code, hide warnings, size a figure. In Quarto these chunk options go on their own lines at the top of the chunk, each starting with #| (hash-pipe):

The most common options:

  • echo: false — run the code but hide it; show only the output. Perfect for a clean report.
  • eval: falseshow the code but don’t run it. Good for illustrating syntax.
  • warning: false, message: false — suppress warnings and messages.
  • label: fig-scores and fig-cap: "..." — name and caption a figure (see the figures lesson).
  • fig-width, fig-height — size a plot in inches.

Try setting echo: false in the summary chunk above and re-running — the code disappears from the output but the result stays.

NoteR Markdown users

In R Markdown, options went inside the chunk header: ```{r echo=FALSE}. Quarto still accepts that, but the #| style is preferred — it’s tidier and works the same across R, Python and Julia.


Next: The YAML header → — the settings block that controls the whole document.