Introduction
to Quarto

Overview

Course materials can be found at:
https://github.com/USFWS/intro-to-quarto .

Outline

  1. Overview of Quarto (30 mins)
  2. YAML metadata (20 min)
  3. Document body (30 min)
  4. Code blocks (30 min)
  5. Final exercise and wrap-up (30 min)

Exercises

QR code to the exercises website.

Sticky notes

Red

“Help!”

A person sitting at a desk at a laptop computer. The laptop has a red sticky note attached to the back of the monitor.

Blue

“All done!”

A person sitting at a desk at a laptop computer. The laptop has a red sticky note attached to the back of the monitor.

The powers of
Superman holding a pencil with city buildings in the background and Quarto written below him

Outputs: Documents

Output: Websites

Outputs: Presentations

Quarto: Under the hood

A diagram showing the process of rendering a document using R Markdown.

A diagram showing the process of rendering a document using R Markdown.

A diagram showing the process of rendering a document using Quarto.

A diagram showing the process of rendering a document using Quarto.

A diagram showing the process of rendering a document using Quarto.

When and why to use Quarto



An cartoon of a snowy plover thinking. There is a question mark above it's head.

When and why to use Quarto



An cartoon of a snowy plover thinking. There is a question mark above it's head.

Advantages of R Markdown


  • Have existing R Markdown code that works for you
  • Custom tools built around R Markdown that are not yet available in Quarto

Advantages of Quarto

  • No requirement for R, fewer package dependencies
  • Revealjs (HTML) slides are easier to work with
  • Better cross referencing of figures, tables, and citations
  • Quarto extensions allow for easier customization
  • Quarto projects make it easier to customize websites, create books, and collaborate
  • It’s the future!



Three ways to render Quarto documents


1.

RStudio GUI A screenshot of a Quarto document in RStudio. The Render button is circled in red

The render icon in RStudio.

2.

Quarto command line interface (CLI) A screenshot of the command line terminal in RStudio. quarto render is typed in.

3.

Quarto R package

quarto::quarto_render(input = ___, 
                      output_format = ___)

Authoring in RStudio

Source editor

A screenshot of a Quarto document in RStudio viewed as Source.

Visual editor

A screenshot of a Quarto document in RStudio viewed as Visual.

Exercise 1: Rendering

QR code to the exercises website.

10:00

Anatomy of a Quarto File

YAML

Yet Another Markup Language = document metadata!

---
title: "Introduction to Quarto"
author: "FWS staff"
format: 
  revealjs:
    theme: [default, refuge_light.scss]
editor: source
---
  • Starts and ends with “---”
  • Uses key-value pairs: key: value that can nest
  • Picky about spacing and indentations!
  • Including options for:
    • Title, authors, date
    • File output(s) (e.g., html, docx)
    • Styling (e.g., theme, fig-width)
    • Parameters
    • And more…

YAML metadata: Options

  • Options are dependent on the file output (many options )
  • Search the Quarto guidance within your specified file format:

YAML metadata: Options

Another option: Use your friendly assistant!

  • Start a word and hit tab to complete or …
  • Type Ctrl+space to see available options.

A screenshot of a yaml for a quarto document in RStudio showing the drop-down menu with options available.

Exercise 2: YAML metadata

QR code to the exercises website.

10:00

Text body: Markdown

  • Markdown is a markup language (e.g., HTML, teX, XML)
    • Add syntax to the text to change it’s look
    • Mix text with markup instructions
  • Quarto uses Pandoc Markdown
    • Inline or block elements
  • Source (Markdown) vs. Visual (WYSIWYM)

Text body

Inline elements

  • Individual words in a sentence
  • Images, links, equations, or code in a sentence

Block elements

  • Paragraphs or sections of a document
  • Standalone images, equations, or figures

Inline elements: Text


Markdown


This is an example of how you could change  
inline elements of text body Markdown in a FWS 
report. You can make a word *italicized* or  
**bold**. You can also display code, such as  
`exp(10) + 1`. You can even ~~strikethrough~~  
and make something a subscript~1~ or
superscript^1^. For more advanced customization, 
you can use [spans]{.mark} to apply 
custom-defined or built-in attributes to 
[inline elements]{style="color:blue;"}.

Output

This is an example of how you could change
inline elements of text body Markdown in a FWS report. You can make a word italicized or
bold. You can also display code, such as
exp(10) + 1. You can even strikethrough
and make something a subscript1 or superscript1. For more advanced customization, you can use spans to apply custom-defined or built-in attributes to inline elements.

Inline elements: Math

with LaTex!

Markdown

The solution to $sqrt(x) = 26$ is $x = 676$ and  
$\pi = 3.1415...$.

Output

The solution to \(sqrt(x) = 26\) is \(x = 676\) and \(\pi = 3.1415...\).

Block elements: Paragraphs

Markdown

Here's a paragraph. To separate it from the next  
paragraph, you need to add one or more empty 
lines.

This is a new paragraph. A paragraph is an  
example  of a block element. You can format each  
paragraph (or a set of paragraphs) independently, 
using divs to group regions of content and apply
styling.

::: {.callout-tip}
## Custom block element  

Here's an example of a block with custom 
formatting. Everything in this block has this 
format. Specifically, this is a "callout block", 
which is a built-in feature of Quarto that uses 
div syntax.
:::

Output

Here’s a paragraph. To separate it from the next paragraph, you need to add one or more empty lines.

This is a new paragraph. A paragraph is an
example of a block element. You can format each
paragraph (or a set of paragraphs) independently, using divs to group regions of content and apply styling.

Custom block element

Here’s an example of a block with custom formatting. Everything in this block has this format. Specifically, this is a “callout block”, which is a built-in feature of Quarto that uses div syntax.

Block elements: Headers


Markdown Syntax Output
# Header 1

Header 1

## Header 2

Header 2

### Header 3

Header 3

#### Header 4

Header 4

Block elements: Lists


Markdown

**Ordered List**

1. This item first
2. Then this one
3. And finally this

 

**Unordered List**

- Kodiak Refuge
- Togiak Refuge
- Arctic Refuge

Output

Ordered List

  1. This item first
  2. Then this one
  3. And finally this

Unordered List

  • Kodiak Refuge
  • Togiak Refuge
  • Arctic Refuge

Block elements: Math

with LaTex!

Markdown

The Cauchy-Schwarz Inequality

$$
\left( \sum_{k=1}^n a_k b_k \right)^2 
\leq 
\left( \sum_{k=1}^n a_k^2 \right) 
\left( \sum_{k=1}^n b_k^2 \right)
$$
is an upper bound on the inner product between  
two vectors in an inner product space in terms  
of the product of the vector norms.

Output

The Cauchy-Schwarz Inequality

\[ \left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right) \] is an upper bound on the inner product between two vectors in an inner product space in terms of the product of the vector norms.

Block elements: Images


Markdown

Here's a paragraph of text. We describe
something of great importance. So great,
in fact, that we need to also include an image. 

![](images/bear_singing.png)

Moving along, we will describe something else. 

Output

Here’s a paragraph of text. We describe
something of great importance. So great,
in fact, that we need to also include an image.

Moving along, we will describe something else.

Exercise 3: Document body

QR code to the exercises website.

10:00

Code chunks: Pandoc


```language
Some code here
```
  • Code chunk surrounded by a “fence” of three backticks ```
  • Specifying the language allows for syntax highlighting.
  • Code is displayed but not executed

Code chunks: R Markdown


```{r, echo=FALSE}
Some code here
```
  • Code chunk surrounded by a “fence” of three backticks ```
  • Specifying the language allows for syntax highlighting.
  • Code is displayed but not executed Code executes unless you specify as an option -> brackets{}
  • Chunk options are a comma-separated list (R syntax)

Code chunks: Quarto


```{language}
#| echo: false

Some code here
```
  • Code chunk surrounded by a “fence” of three backticks ```
  • Specifying the language allows for syntax highlighting.
  • Code is displayed but not executed Code executes unless you specify as an option -> brackets{}
  • Chunk options are a comma-seperated list (R syntax) Chunk options moved to a YAML in the cell after a hash pipe #|
    • Wider language support
    • Easier to read

Code chunks: Labels


  • Identifies code chunks
    • Makes debugging easier
    • Can reference code chunk outputs in the text


```{r}
label: summary-plot

Some code to generate a plot...
```


Avoid using underscores (_) in labels and IDs.

This can cause problems when rendering to PDF with LaTeX.

Execution options: Outputs


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.
include Catch all for preventing any output (code or results) from being included (e.g. include: false suppresses all output from the code block).


Tab Completion is your friend!

Execution options: Figures


```{r}
library(ggplot2)

ggplot(iris, aes(x = Petal.Width, 
                 y = Petal.Length)) +
  geom_point() +
  labs(x = "Petal width",
       y = "Petal length")
```
library(ggplot2)

ggplot(iris, aes(x = Petal.Width, 
                 y = Petal.Length)) +
  geom_point() +
  labs(x = "Petal width",
       y = "Petal length")

Execution options: Figures


```{r}
#| echo: false

library(ggplot2)

ggplot(iris, aes(x = Petal.Width, 
                 y = Petal.Length)) +
  geom_point() +
  labs(x = "Petal width",
       y = "Petal length")
```

Execution options: Figures


```{r}
#| echo: false
#| fig-width: 3
#| fig-height: 2

library(ggplot2)

ggplot(iris, aes(x = Petal.Width, 
                 y = Petal.Length)) +
  geom_point() +
  labs(x = "Petal width",
       y = "Petal length")

```

Execution options: Figures


```{r}
#| echo: false
#| fig-width: 5
#| fig-height: 5

library(ggplot2)

ggplot(iris, aes(x = Petal.Width, 
                 y = Petal.Length)) +
  geom_point() +
  labs(x = "Petal width",
       y = "Petal length")
```

Execution options: Figures


```{r}
#| echo: false
#| fig-width: 5
#| fig-height: 5
#| fig-cap: "This is my really neat plot."

library(ggplot2)

ggplot(iris, aes(x = Petal.Width, 
                 y = Petal.Length)) +
  geom_point() +
  labs(x = "Petal width",
       y = "Petal length")

```

This is my really neat plot.

Execution options: YAML


Execution options at the document level in the YAML

Avoids having to reapply options for each code chunk!

---
title: "My FWS Report"
author: Jane Biologist
format: html
---

Execution options: YAML


Execution options at the document level in the YAML

Avoids having to reapply options for each code chunk!

---
title: "My FWS Report"
author: Jane Biologist
format:
  html:
    fig-width: 5
    fig-height: 5
execute:
  echo: false
---

Inline elements: Code


Source code

This is an example of how you could use
code to calculate and report that the species
in the iris dataset with the longest sepal 
length is the 
`r iris[which.max(iris$Sepal.Length), "Species"]`.

Output

This is an example of how you could use code to calculate and report that species in the iris dataset with the longest sepal length is the virginica.

Exercise 4: Code

QR code to the exercises website.

10:00

Why and HOW Should I Use Quarto?

 

A notebook that says 'What's Next' with a large question mark atop a yellow desk. Licensed under Creative Commons.

Practical Applications for FWS Biologists

  • Generate reusable, automated reports
  • Utilize shareable, standardized templates
  • Annotate code for interpretability, collaboration, and longevity
  • Track and collaborate with version control
  • Seamlessly integrate figures, tables, and citations into your works
  • Create dynamic, interactive visualizations
  • Publish across multiple formats from a single source
  • Build script-based data pipelines - from data collection to reporting

Leading to… efficiency, standardization, reduced error, reproducible science, innovation!

Exercise 5: Your turn

QR code to the exercises website.

10:00

Wrap up

A Quarto spaceship launching into space with documents and reports floating around it.

  • Quarto is a powerful tool for creating scientific documents, presentations, and websites.
  • Three basic components of a Quarto file control the rendered output:
    • YAML metadata
    • Text body
    • Code chunks
  • Once you understand the basics, the sky’s the limit!