This part gives a not so short introduction to Quarto. This introduction is based on the https://quarto.org website. Sometimes the topics are introduced a bit shorter, sometimes complementary information is provided. Most sub-sections provide the hyperlink to the respective Quarto sub-website.
A not so short introduction to Quarto
If you are familiar with RMarkdown (Allaire et al., 2024) the FAQ for RMarkdown Users might be interesting for you.
What is Quarto?
But, what is Pandoc? Pandoc is a universal document converter. It converts files from one markup format into another (e.g., markdown → HTML).
In short, with Quarto you can create different types of (reproducible) documents1 including:
HTML
PDF
MS Word
Markdown
The focus of this workshop is on HTML (and PDF) documents. For an overview about all formats see here.
How to work with Quarto?
- It is basically the same2 as working in regular a R-script.
- Open a new file:
File > New File > Quarto Document...
- Open a new file:
- Provide a title (& author name)
- Save the document with a reasonable name and use the
Renderbutton
- A quarto document roughly consists of three parts:
- The YAML header
- The Body
- Code chunks for programming languages
- The website quarto.org is a great resource to learn it (e.g., follow the guide). It is a daily companion and covers the topics in much greater detail than this page.
The YAML header
The YAML3 header is enclosed by three dashes (---):
Overview of YAML syntax
The basic syntax of YAML uses so-called key-value pairs in the format key: value. This is extremely powerful, because you can generate great documents like this Quarto book without knowing much about HTML, LATEX or programming in general.
Some useful key-value pairs:
subtitle: "my subtitle"
author:
- "John Doe"
- "Jane Roe"
- "Norah Jones"
date: today
toc: true
toc-title: "Inhaltsverzeichnis"
number-sections: true- For the next 5 minutes or so, check out https://quarto.org and identify potential useful
key: valuepairs. Note: There are differentYAMLoptions for the formats: - Add them to your YAML header.
Citing
With Quarto it is possible to automatically generate citations and bibliographies in many different ways.
The key: value pair to include citations in Quarto documents is:
Here we use a so-called BibLaTex (.bib) file. These files can be generated with the most reference management software programs4 (e.g., Citavi, EndNote, zotero, …).
It is highly recommended to maintain your retrieved citations. This means, whenever you download a single (!) reference by means of the doi (Digital Object Identifier), check whether the reference information is correct. This saves a lot of time in the long run.
A reference in the .bib-file looks like this:
@Manual{R-base,
title = {R: A Language and Environment for Statistical Computing},
author = {{R Core Team}},
organization = {R Foundation for Statistical Computing},
address = {Vienna, Austria},
year = {2022},
url = {https://www.R-project.org/},
}- Check the path with the
base::getwd()function (should be the same folder as theQuartodocument, otherwise you have to provide the path in theYAMLbibliographyargument).
- Save (some of the)
Rpackages in a character vector.
- Use the
write_bib()function from theknitrpackage (Xie, 2023).
To change the citation style (e.g., APA, Chicago Manual of Style), we use the csl key5:
The apa.csl file can be found on github.
The citation syntax (How to cite the references in the text?) is briefly explained in the The Body subsection Citation syntax.
Execution/Output options
There are many different options to customize the output of the (executed code).The options can be specified:
- globally (see in the following)
- per code block (see the section on Chunk options)
In the following example, we set echo: false and warning: false.
This means that no code and no warnings are shown, except you state it otherwise in a specific code block.
For more (chunk) options see https://quarto.org/docs/computations/execution-options.html.
The body
As explained in the section What is Quarto?:
Quarto is based on Pandoc and uses its variation of markdown as its underlying document syntax (see https://quarto.org)
This means we can use Markdown syntax6 (together with the information in the YAML header) to generate the different types of documents (e.g., HTML, PDF, …).
The information is very dense, so do not expect to remember everything.
Markdown language
There are plenty of different guides to get a quick overview about the language (see e.g., markdownguide, or Quarto-website). The following code snippets are copied and sometimes slightly adjusted from https://quarto.org.
Text formatting
| Markdown Syntax | Output |
|---|---|
|
italics and bold |
|
superscript2 / subscript2 |
|
|
|
verbatim code |
Headings
To create headings, add one (or more) # in front of the heading.
| Markdown Syntax | Output |
|---|---|
|
Header 1 |
|
Header 2 |
|
Header 3 |
This goes up to level 6 (i.e., ######) which probably is not recommended to use though.
Lists
| Markdown Syntax | Output |
|---|---|
|
|
|
|
More example can be found on https://quarto.org/docs/authoring/markdown-basics.html#lists.
Cross-referencing
To make a figure, table, equation, …, or section/heading referenceable, it is necessary to provide unique identifier. Identifiers must start with their type (e.g., #fig-, #tbl-, #eq-), except for headers (see in the following example):
When we want to cross-reference the Images section, we have to provide unique identifier that is enclosed by braces {} (here: #include-images) after the heading:
An example on how to reference figures is shown in the Images section.
For more on cross-referencing (e.g., how to make sub-figures) see again https://quarto.org/docs/authoring/cross-references.html.
Images
One way to include images is as follows:
The caption [Here goes the caption] and label {#fig-myImage} make this figure referenceable. To reference it, use the following syntax:
It renders to:
See Figure 1 for a graphical representation.
One of them is the include_graphics function from the knitr package (Xie, 2023):
It is referenceable via the label: fig-demo-include. For now ignore the other so-called chunk options (i.e., eval, code-fold). These are explained in the Chunk options section.
For more (e.g., HTML way <img> </img> or <iframe></iframe>) see again https://quarto.org/docs/authoring/figures.html.
Citation syntax
Recall a reference in the .bib-file looks like this:
@Manual{R-base,
title = {R: A Language and Environment for Statistical Computing},
author = {{R Core Team}},
organization = {R Foundation for Statistical Computing},
address = {Vienna, Austria},
year = {2022},
url = {https://www.R-project.org/},
}To cite the R program, use @ before the so-called BibLaTex key R-base. Three example follow:
-
[@R-base]renders as: (R Core Team, 2024) -
@R-baserenders as: R Core Team (2024) -
[-@R-base]renders as: (2024)
Multiple citations are separated by semicolons:
-
[@R-base; @R-knitr]renders as: (R Core Team, 2024; Xie, 2023)
For more information on the citation syntax see the Pandoc Citations documentation.
There is a lot more!
Technical writing (e.g., Equations): Use
$delimiters for inline math and$$delimiters for display math (see https://quarto.org)-
Links:
-
[This is a link to google.de](www.google.de)which appears as This is a link to google.de -
<https://quarto.org>which appears as https://quarto.org
-
Videos
…
Computations
R code blocks (or chunks)
Within a Quarto document source code8 can be displayed by using ``` at the start and the end of the code.
The starting ``` are followed by braces around the programming language (e.g., ```{python})
R code is included by using curly braces around the letter r (i.e., ```{r})
This looks like this:
```{r}
#| label: fig-example-ggplot
#| fig-cap: "My first ggplot"
#| results: hold
library(ggplot2)
ggplot(data = diamonds,
aes(y = carat, x = price, color = cut)) +
geom_point() +
labs(y = "Carat", x = "Price", color = "Cut") +
theme_classic()
```
R code block.
Chunk options9
What are chunk options? Chunk options customize the output of the code blocks. In Quarto it is recommended10 to include the chunk options as special comments (i.e., |#) at the top of the chunk.
In the following example, we set output: false…
… and hence, no output is shown.
The most common chunk options are:
| Chunk option | Description | Value |
|---|---|---|
echo |
Include the source code in output | tr ue/false/fenced |
eval |
Evaluate the code chunk. If false the code of the chunk will not be executed, but depending on the echo value be displayed or not. |
true/false |
include |
Include source code &(!) results in the output. | true/false |
results |
Should results be displayed in the output or not (false)? If yes how (markup vs. asis)? |
ma rkup/asis/hold/ hide/false
|
warnings |
Include warnings in output. | true/false |
Rendering
There are a couple of ways to render a Quarto document:
Clicking the Render Button or using the keyboard shortcut (windows):
Ctrl+Shift+K11-
Using the terminal
quarto render script.qmd --to htmlquarto render script.qmd --to pdf- …
Using the
quarto_renderfunction from thequarto(Allaire & Dervieux, 2024) package
When rendering a Quarto document, the R code chunks are executed (except you stated: eval: false).
References
Footnotes
Note: It is also possible to generate, presentations (e.g., powerpoint, beamer), websites, books and even interactive components.↩︎
Nevertheless, it (probably) will take some time to get familiar with it↩︎
YAML is an acronym for Yet Another Markup Language see https://en.wikipedia.org/wiki/YAML↩︎
If you do not use one, it is time now.↩︎
CSL is the abbreviation for Citation Style Language (for more see https://en.wikipedia.org/wiki/Citation_Style_Language↩︎
It is also possible to use
LatexorHTMLsyntax. However, you most likely run into problems when rendering into the respective other type.↩︎Quarto/Pandocsupports displaying (!) many different programming languages (see here)↩︎see also here: https://yihui.org/knitr/options/↩︎
The
rmarkdownsyntax is different (e.g.,{r my-label, echo = FALSE}), but it also works inQuarto.↩︎For Mac it is:
Cmd+Shift+K↩︎