Intro to Quarto







R-Ladies Baltimore | March 12, 2024

Isabella Velásquez

Introduction

Agenda

  1. Introduction

  2. Setting Up Quarto

  3. Getting Started with Quarto

  4. Creating Projects

  5. Styling Quarto

  6. Publishing Quarto

  7. Conclusion

Hello!

My name is Isabella Velásquez.

@ivelasq

@ivelasq3

@ivelasq3

ivelasq.rbind.io

Resources

  • Slides: https://ivelasq.quarto.pub/intro-to-quarto-2024/
  • Exercise repo: https://github.com/ivelasq/intro-to-quarto-exercises
  • rstudio.cloud Project: https://rstudio.cloud/content/4823293

Requirements

To follow along with the exercises, please be sure that you:

  • Are on the latest version of RStudio v2023.12 or later and have cloned the exercise repo
  • Or, have a rstudio.cloud account and can access the project
  • And, have a https://quartopub.com/ account.

What is Quarto®?

Quarto is an

open-source

scientific and technical

publishing system

built on Pandoc.

Quarto Origins

  • At its core, Quarto is multilingual and independent of computational systems
  • Has expanded upon R Markdown to add new languages and can add more in the future


Let’s bring R Markdown to everyone!

Quarto Engines

Quarto Workflow



Quarto Engines

knitr

---
title: "ggplot2 demo"
format: 
  html:
    code-fold: true
---

## Meet Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

```{r}
#| label: plot-penguins
#| echo: false
#| message: false
#| warning: false

library(tidyverse)
library(palmerpenguins)

ggplot(penguins, 
       aes(x = flipper_length_mm, y = bill_length_mm)) +
  geom_point(aes(color = species, shape = species)) +
  scale_color_manual(values = c("darkorange","purple","cyan4")) +
  labs(
    title = "Flipper and bill length",
    subtitle = "Dimensions for penguins at Palmer Station LTER",
    x = "Flipper length (mm)", y = "Bill length (mm)",
    color = "Penguin species", shape = "Penguin species"
  ) +
  theme_minimal()
```

Quarto Engines

Jupyter

---
title: "Palmer Penguins Demo"
format: 
    html:
        code-fold: true
jupyter: python3
---

## Meet Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

```{python}
#| echo: false
#| message: false

import pandas as pd
import seaborn as sns 
from palmerpenguins import load_penguins
sns.set_style('whitegrid')

penguins = load_penguins()

g = sns.lmplot(x="flipper_length_mm",
               y="body_mass_g",
               hue="species",
               height=7,
               data=penguins,
               palette=['#FF8C00','#159090','#A034F0']);
g.set_xlabels('Flipper Length');
g.set_ylabels('Body Mass');
```

Quarto Formats

Websites

---
title: Home
pagetitle: nbdev – Create delightful software with Jupyter Notebooks
page-layout: custom
section-divs: false
css: index.css
toc: false
image: https://nbdev.fast.ai/images/card.png
description: Write, test, document, and distribute software packages and technical articles —        all in one place, your notebook.
---

from fastcore.foundation import L
from nbdev import qmd

def img(fname, classes=None, **kwargs): return qmd.img(f"images/{fname}", classes=classes,     **kwargs)
def btn(txt, link): return qmd.btn(txt, link=link, classes=['btn-action-primary', 'btn-action',     'btn', 'btn-success', 'btn-lg'])
def banner(txt, classes=None, style=None): return qmd.div(txt, L('hero-banner')+classes,     style=style)

features = L(
    ('docs', 'Beautiful technical documentation and scientific articles with Quarto'),
    ('testing', 'Out-of-the-box continuous integration with GitHub Actions'),
    ('packaging', 'Publish code to PyPI and conda, and prose to GitHub Pages'),
    ('vscode', 'Two-way sync with your favourite IDEs'),
    ('jupyter', 'Write prose, code, and tests in notebooks — no context-switching'),
    ('git', 'Git-friendly notebooks: human-readable merge conflicts; no unwanted metadata')
)

nbdev.fast.ai

Quarto Formats

Blogs

https://jollydata.blog/

Books

Python for Data Analysis, 3E by Wes McKinney

Quarto Formats

Presentations

The untold story of palmerpenguins by Dr. Kristen Gorman, Dr. Allison Horst, and
Dr. Alison Hill

Journals

Journal of Statistical Software (JSS)

Quarto Formats

Dashboards (NEW!)

Dashboard by Mine Çetinkaya-Rundel

Why Quarto?

A unified ecosystem

  • R Markdown has a lot of packages doing lots of different things

  • Quarto has a shared expression for core features

Why Quarto?

New features

  • Cross references
  • Advanced layout
  • Figure/layout panels
  • Callouts
  • Diagrams
  • Extensions
  • Interactivity
  • YAML intelligence
  • Publishing
  • Conditional content
  • Notebook filters

Why Quarto?

Lua-based extension system

nutshell shortcode by schochastics

Journal of Statistical Software (JSS)

nes-revealjs format by EmilHvitfeldt

attribution Revealjs Extension

Why Quarto?

webR

Run R in your Quarto documents thanks to the quarto-webr extension by James Joseph Balamuta.

Why Quarto?

Shinylive

With Shinylive, you can embed Shiny for Python applications into Quarto documents and run the entire application inside the user’s web browser.

Examples on the Shiny Website

Setting Up Quarto

Agenda

  1. Introduction

  2. Setting Up Quarto

  3. Getting Started with Quarto

  4. Creating Projects

  5. Styling Quarto

  6. Publishing Quarto

  7. Conclusion

Quarto Installation

Quarto Tooling

Terminal
quarto render

RStudio

To create a new document in RStudio, go to File > New File > Quarto Document:

Working in RStudio

Rendering

Use the Render button () in the RStudio IDE to render the file and preview the output with a single click or keyboard shortcut (K).

Automatically render on save by checking the Render on Save option:

YAML Intelligence

YAML code completion is available for project files, YAML front matter, and executable cell options:

If you have incorrect YAML it will also be highlighted when documents are saved:

Working with the RStudio Visual Editor

Getting Started with Quarto

Agenda

  1. Introduction

  2. Setting Up Quarto

  3. Getting Started with Quarto

  4. Creating Projects

  5. Styling Quarto

  6. Publishing Quarto

  7. Conclusion

Contents of a Quarto Document

YAML

  • “Yet another markup language”
  • Metadata of your document
  • Demarcated by three dashes (---) on either end
  • Uses key-value pairs in the format key: value

YAML

---
title: "Penguins, meet Quarto!"
format: html
editor: visual
---

YAML

---
title: "Penguins, meet Quarto!"
subtitle: "Intro to Quarto Exercise"
format: html
editor: visual
---

All YAML fields for HTML documents

Code Chunks

  • Code chunks begin and end with three backticks (usually)
  • Code chunks are identified with a programming language in between {}
  • Can include optional chunk options, in YAML style, identified by #| at the beginning of the line

Code Chunks

```{r, label="plot-penguins", warning=FALSE, echo=FALSE}
ggplot(penguins, 
       aes(x = flipper_length_mm, y = bill_length_mm)) +
  geom_point(aes(color = species, shape = species)) +
  scale_color_manual(values = c("darkorange","purple","cyan4")) +
  labs(
    title = "Flipper and bill length",
    subtitle = "Dimensions for penguins at Palmer Station LTER",
    x = "Flipper length (mm)", y = "Bill length (mm)",
    color = "Penguin species", shape = "Penguin species"
  ) +
  theme_minimal()
```

Code Chunks

```{r}
#| label: plot-penguins
#| warning: false
#| echo: false

ggplot(penguins, 
       aes(x = flipper_length_mm, y = bill_length_mm)) +
  geom_point(aes(color = species, shape = species)) +
  scale_color_manual(values = c("darkorange","purple","cyan4")) +
  labs(
    title = "Flipper and bill length",
    subtitle = "Dimensions for penguins at Palmer Station LTER",
    x = "Flipper length (mm)", y = "Bill length (mm)",
    color = "Penguin species", shape = "Penguin species"
  ) +
  theme_minimal()
```

Code Chunks

```{r}
#| label: plot-penguins
#| warning: false
#| echo: false
#| fig-alt: "Scatterplot with flipper length in millimeters on
#|  the x-axis, bill length in millimeters on the y-axis, colored
#|  by species, showing a slightly positive relationship with
#|  Chinstrap penguins having higher bill length but lower body
#|  mass, Adelie with low bill length and low body mass, and
#|  Gentoo with high body mass and high bill length."

ggplot(penguins, 
       aes(x = flipper_length_mm, y = bill_length_mm)) +
  geom_point(aes(color = species, shape = species)) +
  scale_color_manual(values = c("darkorange","purple","cyan4")) +
  labs(
    title = "Flipper and bill length",
    subtitle = "Dimensions for penguins at Palmer Station LTER",
    x = "Flipper length (mm)", y = "Bill length (mm)",
    color = "Penguin species", shape = "Penguin species"
  ) +
  theme_minimal()
```

Code Chunks

Code Chunks

Cross referencing

```{r}
#| label: fig-penguins
#| warning: false

ggplot(penguins,
       aes(x = flipper_length_mm, y = bill_length_mm)) +
  geom_point(aes(color = species, shape = species)) +
  scale_color_manual(values = c("darkorange", "purple", "cyan4")) +
  labs(
    title = "Flipper and bill length",
    subtitle = "Dimensions for penguins at Palmer Station LTER",
    x = "Flipper length (mm)",
    y = "Bill length (mm)",
    color = "Penguin species",
    shape = "Penguin species"
  ) +
  theme_minimal()
```
See @fig-penguins.

Code Chunks

Cross referencing

Multiple Figures

Put two plots side by side:

```{r}
#| layout-ncol: 2
#| warning: false

ggplot(penguins, 
       aes(x = flipper_length_mm, y = bill_length_mm)) +
  geom_point(aes(color = species, shape = species))

ggplot(data = penguins, aes(x = flipper_length_mm)) +
  geom_histogram(aes(fill = species), 
                 alpha = 0.5, 
                 position = "identity")

```

Multiple Figures

Put two plots side by side:

Multiple Figures

Put two plots side by side:

```{r}
#| layout: "[[30, 70]]"
#| warning: false

ggplot(penguins, 
       aes(x = flipper_length_mm, y = bill_length_mm)) +
  geom_point(aes(color = species, shape = species))

ggplot(data = penguins, aes(x = flipper_length_mm)) +
  geom_histogram(aes(fill = species), 
                 alpha = 0.5, 
                 position = "identity")

```

Code Options for All Chunks

Use the YAML to control options for all code chunks.

Hide all of the code and just show the output by specifying echo: false within the execute option in the YAML.

Echo

One chunk

```{r}
#| label: plot-penguins
#| echo: false

ggplot(penguins, 
       aes(x = flipper_length_mm, y = bill_length_mm)) +
  geom_point(aes(color = species, shape = species)) +
  scale_color_manual(values = c("darkorange","purple","cyan4")) +
  labs(
    title = "Flipper and bill length",
    subtitle = "Dimensions for penguins at Palmer Station LTER",
    x = "Flipper length (mm)", y = "Bill length (mm)",
    color = "Penguin species", shape = "Penguin species"
  ) +
  theme_minimal()
```

All chunks

```{yaml}
---
title: "Hello, Quarto!"
format: html
editor: visual
execute:
  echo: false
---
```

Code Options for All Chunks

Code Folding

```{yaml}
---
title: "Hello, Quarto!"
format:
  html:
    code-fold: true
---
```

Code Tools

```{yaml}
---
title: "Hello, Quarto!"
format:
  html:
    code-tools:
      source: https://quarto.org
---
```

Code Options for All Chunks

Code Linking

Needs downlit package

```{yaml}
---
title: "Hello, Quarto!"
format:
  html:
    code-link: true
---
```

Markdown Text

  • Markdown is a lightweight language for creating formatted text
  • Quarto is based on Pandoc and uses its variation of markdown as its underlying document syntax

Markdown Text


The `penguins` data from the [**palmerpenguins**](https://allisonhorst.github.io/palmerpenguins "palmerpenguins R package") package contains size measurements for `r nrow(penguins)` penguins from three species observed on three islands in the Palmer Archipelago, Antarctica.

The penguins data from the palmerpenguins package contains size measurements for 344 penguins from three species observed on three islands in the Palmer Archipelago, Antarctica.

Markdown Text

Formatting

Markdown Syntax Output
*italics* and **bold**
italics and bold
superscript^2^ / subscript~2~
superscript2 / subscript2
~~strikethrough~~
strikethrough
`verbatim code`
verbatim code

Markdown Text

Markdown Syntax Output
<https://quarto.org>
https://quarto.org
[Quarto](https://quarto.org)
Quarto
![](penguin.jpg)

Markdown Text

Tables

```{markdown}
| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
|    12 | 12   | 12      |   12   |
|   123 | 123  | 123     |  123   |
|     1 | 1    | 1       |   1    |
```
Right Left Default Center
12 12 12 12
123 123 123 123
1 1 1 1

Markdown Text

Tables

```{markdown}
| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
|    12 | 12   | 12      |   12   |
|   123 | 123  | 123     |  123   |
|     1 | 1    | 1       |   1    |

: Table Column Widths {tbl-colwidths="[10,30,30,30]"}
```
Table Column Widths
Right Left Default Center
12 12 12 12
123 123 123 123
1 1 1 1

Quarto guide for Markdown basics

Quarto Authoring

Diagrams

Quarto has native support for embedding Mermaid and Graphviz diagrams.

```{mermaid}
%%| fig-width: 6
flowchart LR
  A[1] --> B(2)
  B --> C{3}
  C --> D[4]
  C --> E[5]
```

flowchart LR
  A[1] --> B(2)
  B --> C{3}
  C --> D[4]
  C --> E[5]

Quarto Authoring

Equations

```{markdown}
$$E = mc^{2}$$
```

\[E = mc^{2}\]

Quarto Authoring

Title Banners

HTML pages rendered with Quarto include a formatted title banner at the start of the article.

Title Banners

---
title: "Penguins, meet Quarto!"
format: html
editor: visual
---

Toggle on…

Title Banners

---
title: "Penguins, meet Quarto!"
title-block-banner: true
format: html
editor: visual
---

Title Banners

Use a color…

---
title: "Penguins, meet Quarto!"
title-block-banner: "#FDA172"
format: html
editor: visual
---

Title Banners

Or a photo!

---
title: "Penguins, meet Quarto!"
title-block-banner: "banner-image.jpg"
format: html
editor: visual
---

All title block metadata

Divs and Spans

You can add classes, attributes, and other identifiers to content using Divs and Spans.

Divs

::: {.border}
This content can be styled with a border
:::

Spans

[This is *some text*]{.class key="val"}

Divs

  • ::: - Div start and end
  • #id - label
  • .class - behavior
  • attribute=value - customization

Divs

Callout Blocks

:::{.callout-tip}

Note that there are five types of callouts, including: 
`note`, `tip`, `warning`, `caution`, and `important`.

:::

. . .

Tip

Note that there are five types of callouts, including: note, tip, warning, caution, and important.

Divs

Callout Blocks

::: {#call1 .callout-note appearance="simple"}

## Pay Attention

Using callouts is an effective way to highlight content that your reader give special consideration or attention.

:::

Pay Attention

Using callouts is an effective way to highlight content that your reader give special consideration or attention.

Divs

Callout Blocks

Warning

.callout-warning

Caution

.callout-caution

Important

.callout-important

Divs

Multiple columns

```{markdown}
::: {layout-ncol=2}

![](penguin.jpg)

Photo by <a href="https://unsplash.com/@corneliusventures?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Cornelius Ventures</a> on <a href="https://unsplash.com/s/photos/penguin?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
:::
```

. . .

Divs

Multiple columns

```{markdown}
::: {layout="[[1,1], [1]]"}
![penguin 1](penguin.jpg)

![penguin 2](penguin.jpg)

![penguin 3](penguin.jpg)
:::
```

penguin 1

penguin 2

penguin 3

Divs

Tabsets

```{markdown}
::: {.panel-tabset group="language"}
## R

`library(dplyr)`

## Python

`import pandas as pd`
:::
```

library(dplyr)

import pandas as pd

Spans

  • [] and {} for span start and end
  • .class - behavior
  • key="val" - customization

Spans

```{markdown}
This is text that is [red]{style="color:red;"}.
```

This is text that is red.

Spans

```{markdown}
![](penguin.jpg){fig-alt="A photo of a penguin jumping"}
```

A photo of a penguin jumping

```{markdown}
![](penguin.jpg){fig-alt="A photo of a penguin jumping" width=100}
```

A photo of a penguin jumping

Other formats

You can create Revealjs presentations using the revealjs format.

```{yaml}
---
title: "Penguins, meet Quarto!"
format: revealjs
editor: visual
---
```

Revealjs

Code Line Numbers

```{r}
#| echo: true
#| eval: FALSE
#| code-line-numbers: 3-4

ggplot(penguins, 
       aes(x = flipper_length_mm, y = bill_length_mm)) +
  geom_point(aes(color = species, shape = species)) +
  scale_color_manual(values = c("darkorange","purple","cyan4")) 
```


ggplot(penguins, 
       aes(x = flipper_length_mm, y = bill_length_mm)) +
  geom_point(aes(color = species, shape = species)) +
  scale_color_manual(values = c("darkorange","purple","cyan4")) 

Revealjs

Incremental

```{markdown}
::: {.incremental}
- Adelie
- Chinstrap
- Gentoo
:::
```
  • Adelie
  • Chinstrap
  • Gentoo

All about revealjs in Quarto

Creating Projects

Agenda

  1. Introduction

  2. Setting Up Quarto

  3. Getting Started with Quarto

  4. Creating Projects

  5. Styling Quarto

  6. Publishing Quarto

  7. Conclusion

Quarto Projects

Quarto projects are directories that provide:

  • A way to render all or some of the files in a directory with a single command (e.g. quarto render myproject).
  • A way to share YAML configuration across multiple documents.
  • The ability to redirect output artifacts to another directory.
  • The ability to freeze rendered output (i.e. don’t re-execute documents unless they have changed).

Quarto Projects

Setup

```{yaml}
project:
  output-dir: _output

toc: true
number-sections: true
bibliography: references.bib  
  
format:
  html:
    css: styles.css
    html-math-method: katex
  pdf:
    documentclass: report
    margin-left: 30mm
    margin-right: 30mm
```

Quarto Projects

Freeze

```{yaml}
execute:
  freeze: true  # never re-render during project render
```


```{yaml}
execute:
  freeze: auto  # re-render only when source changes
```

Styling Quarto

Agenda

  1. Introduction

  2. Setting Up Quarto

  3. Getting Started with Quarto

  4. Creating Projects

  5. Styling Quarto

  6. Publishing Quarto

  7. Conclusion

HTML Theming

Quarto includes 25 themes from the Bootswatch project:

  • default
  • cerulean
  • cosmo
  • cyborg
  • darkly
  • flatly
  • journal
  • litera
  • lumen
  • lux
  • materia
  • minty
  • morph
  • pulse
  • quartz
  • sandstone
  • simplex
  • sketchy
  • slate
  • solar
  • spacelab
  • superhero
  • united
  • vapor
  • yeti
  • zephyr

HTML Theming

Use these themes under the theme option in the YAML:

```{yaml}
format:
  html:
    theme: flatly
```

HTML Theming

You can do extensive additional customization using SASS theme files.

  • Bootstrap defines over 1,400 variables that control fonts, colors, padding, borders, and much more!
```{css}
/*-- scss:defaults --*/

$body-bg: #CDEEFD;

/*-- scss:rules --*/
h1, h2, h3, h4, h5, h6 {
  text-shadow: -1px -1px 0 rgba(0, 0, 0, .3);
}
```

HTML Theming

Provide the custom theme under theme:

```{yaml}
theme:
  - flatly
  - custom.scss
```

HTML Theming

Quarto also allows you to use a dark and light theme.

```{yaml}
theme:
  light: flatly
  dark: darkly
```

Publishing Quarto

Agenda

  1. Introduction

  2. Setting Up Quarto

  3. Getting Started with Quarto

  4. Creating Projects

  5. Styling Quarto

  6. Publishing Quarto

  7. Conclusion

Publishing options

Since content rendered with Quarto uses standard formats (HTML, PDFs, MS Word, etc.) it can be published anywhere.

Some options:

  • Quarto Pub
  • GitHub Pages
  • RStudio Connect
  • Netlify

Quarto Pub

Quarto Pub is a free publishing service for content created with Quarto.

  • Must be publicly available
  • Cannot be larger than 100 MB
  • Has a soft limit of 10 GB a month
Terminal
quarto publish quarto-pub hello.qmd


Terminal
# token created at https://quartopub.com/profile/
export QUARTO_PUB_AUTH_TOKEN="qpa_k4yWKEmlu5wkvx173Ls"

# publish to quarto-pub site specified within _publish.yml
quarto publish quarto-pub

Conclusion

Agenda

  1. Introduction

  2. Setting Up Quarto

  3. Getting Started with Quarto

  4. Creating Projects

  5. Styling Quarto

  6. Publishing Quarto

  7. Conclusion

Thank you!

I hope that you enjoyed this intro to Quarto!

Acknowledgements