Reproducible publishing with Quarto
Duke University
Posit, PBC
Posit, PBC
2024-08-04
Option 1: Start the project 2-documents-slides
.
Option 2: Launch the project in 2-documents-slides
.
Metadata: YAML
Text: Markdown
Code: Executed via knitr
or jupyter
Weave it all together, and you have beautiful, powerful, and useful outputs!
Literate programming is writing out the program logic in a human language with included (separated by a primitive markup) code snippets and macros.
“Yet Another Markup Language” or “YAML Ain’t Markup Language” is used to provide document level metadata.
Indentation matters!
:
There are multiple ways of formatting valid YAML:
:
:
format: html
with selections made with proper indentationTo avoid manually typing out all the options, every time when rendering via the CLI:
Lint, or a linter, is a static code analysis tool used to flag programming errors, bugs, stylistic errors and suspicious constructs.
RStudio + VSCode provide rich tab-completion - start a word and tab to complete, or Ctrl + space
to see all available options.
Option 1: Start the project 2-documents-slides
.
Option 2: Launch the project in 2-documents-slides
.
hello-penguins.qmd
in RStudio.Ctrl + space
to see the available YAML options.03:00
Many YAML fields are common across various outputs
But also each output type has its own set of valid YAML fields and options
Definitive list: quarto.org/docs/reference/formats/html
The markdown you know from R Markdown will work in Quarto.
Markdown Syntax | Output |
---|---|
|
italics and bold |
|
superscript2 / subscript2 |
|
|
|
verbatim code |
Markdown Syntax | Output |
---|---|
|
Header 1 |
|
Header 2 |
|
Header 3 |
|
Header 4 |
|
Header 5 |
|
Header 6 |
There are several types of “links” or hyperlinks.
Markdown
You can embed [named hyperlinks](https://quarto.org/),
direct urls like <https://quarto.org/>, and links to
[other places](#quarto-anatomy) in
the document.
The syntax is similar for embedding an
inline image: ![Penguins playing with ball](images/penguins-quarto-ball.png).
Output
You can embed named hyperlinks, direct urls like https://quarto.org/, and links to other places in the document.
The syntax is similar for embedding an inline image: .
Unordered list:
Output
Ordered list:
Markdown:
> Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.
> - Donald Knuth, Literate Programming
Output:
Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do. - Donald Knuth, Literate Programming
markdown-syntax.qmd
in RStudio.08:00
Pandoc, and therefore Quarto, can parse “fenced div blocks”:
:::
div as a HTML <div>
but it can also apply in specific situations to content in PDF:This content can be styled with a border
These can often apply between formats:
Single class: Two equivalent syntaxes
Multiple classes: use {
and .
, separate with spaces
::: 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
.
Warning
Callouts provide a simple way to attract attention, for example, to this warning.
Important
Danger, callouts will really improve your writing.
Caution
Here is something under construction.
Caption
Tip with caption.
callout-boxes.qmd
and render the document.03:00
Pandoc supports numbering and formatting footnotes.
Here is an inline note.^[Inlines notes are easier to write,
since you don't have to pick an identifier and move down to
type the note.]
Here is an inline note.1
Here is an footnore reference[^1]
[^1]: This can be easy in some situations when you have a really long note or
don't want to inline complex outputs.
Here is an footnote reference1
Notice in both situations that the footnote is placed at the bottom of the page in presentations, whereas in a document it would be hoverable or at the end of the document.
r
) is indicated between curly braces {r}
#|
(hashpipe): #| option1: value
Available code cell options: https://quarto.org/docs/reference/cells/cells-knitr.html
If you simply want code formatting but don’t want to execute the code:
```r
```r
head(penguins)
```
echo
The echo
option shows the code when set to true
and hides it when set to false
.
If you want to both execute the code and return the full code including backticks (like in a teaching scenario) echo: fenced
is your friend!
In reproducible reports and manuscripts, the most commonly included code outputs are tables and figures.
So they get their own special sections in our deep dive!
Markdown:
| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
| 12 | 12 | 12 | 12 |
| 123 | 123 | 123 | 123 |
| 1 | 1 | 1 | 1 |
Output:
Right | Left | Default | Center |
---|---|---|---|
12 | 12 | 12 | 12 |
123 | 123 | 123 | 123 |
1 | 1 | 1 | 1 |
Markdown:
+---------------+---------------+--------------------+
| Fruit | Price | Advantages |
+===============+===============+====================+
| Bananas | $1.34 | - built-in wrapper |
| | | - bright color |
+---------------+---------------+--------------------+
| Oranges | $2.10 | - cures scurvy |
| | | - tasty |
+---------------+---------------+--------------------+
: Sample grid table.
Output:
Fruit | Price | Advantages |
---|---|---|
Bananas | $1.34 |
|
Oranges | $2.10 |
|
+---------------+---------------+--------------------+
| Right | Left | Centered |
+==============:+:==============+:==================:+
| Bananas | $1.34 | built-in wrapper |
+---------------+---------------+--------------------+
+--------------:+:--------------+:------------------:+
| Right | Left | Centered |
+---------------+---------------+--------------------+
Note that grid tables are quite awkward to write with a plain text editor because unlike pipe tables, the column indicators must align.
The Visual Editor can assist in making these tables!
The knitr package can turn data frames into tables with knitr::kable()
:
species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
---|---|---|---|---|---|---|---|
Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
Adelie | Torgersen | 40.3 | 18.0 | 195 | 3250 | female | 2007 |
Adelie | Torgersen | NA | NA | NA | NA | NA | 2007 |
Adelie | Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 |
Adelie | Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 |
If you want fancier tables, try the gt package and all that it offers!
species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
---|---|---|---|---|---|---|---|
Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
Adelie | Torgersen | 40.3 | 18.0 | 195 | 3250 | female | 2007 |
Adelie | Torgersen | NA | NA | NA | NA | NA | 2007 |
Adelie | Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 |
Adelie | Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 |
![Penguins playing with a Quarto ball](images/penguins-quarto-ball.png)
![Penguins playing with a Quarto ball](images/penguins-quarto-ball.png){fig-align="left"}
![](images/penguins-quarto-ball.png){fig-align="right" fig-alt="Illustration of two penguins playing with a Quarto ball."}
Markdown:
::: {#fig-penguins layout-ncol=2}
![Blue penguin](images/blue-penguin.png){#fig-blue width="250px"}
![Orange penguin](images/orange-penguin.png){#fig-orange width="250px"}
Two penguins
:::
Output:
Markdown:
Output:
Last paragraph in the div block is used as the figure caption.
In places like markdown, YAML, or the command line/shell/terminal, you’ll need to use absolute or relative file paths:
"/Users/mine/quarto-asa-nebraska"
- Whose computer will this work on?Relative = BETTER:
"../
= up one directory, ../../
= up two directories, etc./..
or /
= start from root
directory of your current computerHelp readers to navigate your document with numbered references and hyperlinks to entities like figures and tables.
Cross referencing steps:
fig-
or tbl-
.@fig-...
or @tbl-...
.The presence of the caption (Blue penguin
) and label (#fig-blue-penguin
) make this figure referenceable:
Markdown:
See @fig-blue-penguin for a cute blue penguin.
![Blue penguin](images/blue-penguin.png){#fig-blue-penguin}
Output:
See Figure 3 for a cute blue penguin.
The presence of the caption (A few penguins
) and label (#tbl-penguins
) make this table referenceable:
Markdown:
Output:
See Table 1 for data on a few penguins.
species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
---|---|---|---|---|---|---|---|
Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
Adelie | Torgersen | 40.3 | 18.0 | 195 | 3250 | female | 2007 |
Adelie | Torgersen | NA | NA | NA | NA | NA | 2007 |
Adelie | Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 |
Adelie | Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 |
tables-figures.qmd
.10:00
Metadata: YAML
Text: Markdown
Code: Executed via knitr
or jupyter
Weave it all together, and you have a beautiful, functional slide deck!
Let’s build a presentation together from hello-penguins-slides.qmd
and showcase the following features of Quarto presentations:
Hierarchy, headers, and document outline
Incremental lists
Columns
Code, output location, code highlighting
Logo and footer
Making things fit on a slide
Chalkboard
Publishing your presentation to Quarto Pub
Pick up where we left off and
Change the transition style between slides
Change the slide size
Add slide numbers
10:00
Let’s continue building our a presentation together from hello-penguins-slides.qmd
and showcase the following features of Quarto presentations:
Fragments
Title slide attributes
revealjs
- essentially the replacement for xaringan
, but with Pandoc-native syntax
beamer
for LaTeX slides
PowerPoint for when you have to collaborate via MS Office
Any questions / anything you’d like to review before we wrap up this module?