Literate programming in Quarto

What goes below the YAML?

Quarto, like R Markdown, allows you to do literate programming.

Literate programming is writing out the program logic in a human language with included (separated by a primitive markup) code snippets and macros.

Quarto is based on Pandoc and uses its variation of markdown as its underlying document syntax.

Autonomy of a Quarto document

A Quarto document

YAML

Text

Code chunks

Markdown basics

Quarto is based on Pandoc and uses its variation of markdown as its underlying document syntax:

Reminder that the visual editor mode in RStudio provides features for authoring in Markdown.

Text Formatting

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

Headings

Markdown Syntax Output
# Header 1

Header 1

## Header 2

Header 2

### Header 3

Header 3

#### Header 4

Header 4

##### Header 5
Header 5
###### Header 6
Header 6

Source Code

Use ``` to delimit blocks of source code:

```
code
```

Add syntax highlight code blocks by specifying a language:

```python
1 + 1
```

Fenced divs

You can add classes, attributes, and other identifiers to regions of content using divs and spans:

Borders

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

This content can be styled with a border

Callout notes

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

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

Tabsets

::: {.panel-tabset}
## Tab 1

Hello!

## Tab 2

Hello again!
:::

Hello!

Hello again!

Quarto authoring documentation

Using R/Python/Julia/Observable

Quarto supports executable code blocks within markdown.

```{r}
mtcars |> with(plot(hp, mpg))
```

Calling engines

  • Need to have the appropriate engine installed.
  • Quarto can guess the engine or you can call it explicitly in the YAML.
---
title: "matplotlib demo"
format:
  html:
    code-fold: true
jupyter: python3
---
---
title: "ggplot2 demo"
author: "Norah Jones"
date: "5/22/2021"
format: 
  html:
    fig-width: 8
    fig-height: 4
    code-fold: true
---

Code Blocks

Code blocks that use braces around the language name (e.g. {r}) are executable, and will be run by Quarto during render.

library(ggplot2)
ggplot(airquality, aes(Temp, Ozone)) + 
  geom_point() + 
  geom_smooth(method = "loess"
)

Figure 1: Temperature and ozone level.

Execution options

You can customize executed code with execution options. They can be specified globally:

---
title: "My Document"
execute:
  echo: false
jupyter: python3
---

Or on a per-chunk basis:

```{python}
#| echo: true

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()
```

Note the use of the hashpipe #| at the top of the chunk.

Quarto execution options documentation

Rendering documents

To render, options include:

  • Use the RStudio “Render” button or the “Build” tab
  • Use the command line interface
quarto render
Try it out!

rstd.io/quarto-blog-exercise-repo
Launch RStudio Cloud rstd.io/quarto-blog-exercise-cloud

In the /post/my-new-post/index.html file of your Quarto blog:

  • Add a heading 1
  • Write some text underneath, including a hyperlink.
  • Add a tabset.
  • Include this code chunk:
```{r}
mtcars |> with(plot(hp, mpg))
```

Then, render the blog.