Quarto implements the concept of literate programming.
Literate programming is a programming paradigm introduced in 1984 by Donald Knuth in which a computer program is given as an explanation of how it works in a natural language, such as English, interspersed (embedded) with snippets of macros and traditional source code, from which compilable source code can be generated.
Initial concept
Modern implementation with Quarto
Multiple outputs
Quarto provides a unified authoring framework for data science, combining your code, its results, and your prose.
Quarto documents are fully reproducible, they automate the inclusion of the last versions of the results of an analysis, literally dozens of output formats are available: Web pages, PDFs, Word files, websites, books, and more.
Relevant to many use cases
According to Hadley Wickham Quarto files are designed to be used in three ways:
For communicating to decision-makers or to a more general audience, who want to focus on the conclusions, not the code behind the analysis.
For collaborating with other data scientists (including future you!), who are interested in both your conclusions, and how you reached them (i.e. the code).
As an environment in which to do data science, as a modern-day lab notebook where you can capture not only what you did, but also what you were thinking. Recall Galileo and his notebook.
Tailored to modern data science
Quarto is a language agnostic command line interface (CLI)
me@my_beautiful_linux_computer:~$ quarto --help
Usage: quartoVersion: 1.4.549Description:quarto CLIOptions:-h,--help- Show this help. -V,--version- Show the version number for this program. Commands:render[input][args...]- Render files or projects to various document types.preview[file][args...]- Render and preview a document or website project. serve[input]- Serve a Shiny interactive document. create[type][commands...]- Create a `Quarto` project or extension use<type>[target]- Automate document or project setup tasks. add<extension> - Add an extension to this folder or project update[target...]- Updates an extension or global dependency. remove[target...]- Removes an extension. convert<input> - Convert documents to alternate representations. pandoc[args...]- Run the version of Pandoc embedded within `Quarto`. typst[args...]- Run the version of Typst embedded within `Quarto`. run[script][args...]- Run a TypeScript, R, Python, or Lua script. install[target...]- Installs a global dependency (TinyTex or Chromium).uninstall[tool]- Removes an extension. tools- Display the status of `Quarto` installed dependenciespublish[provider][path]- Publish a document or project to a provider. check[target]- Verify correct functioning of `Quarto` installation. help[command]- Show this help or the help of a sub-command.
The machinery
The machinery
The machinery
Meet Quarto
Authoring Quarto documents
When working with R using Rstudio, Quarto comes pre-installed with recent versions of Rstudio.
To create a new document in RStudio, go to File > New File > Quarto Document:
Use the Render button in the RStudio IDE to render the file and preview the output with a single click or keyboard shortcut (⌘ / ctrl⇧K).
Automatically render on save by checking the Render on Save option:
Quarto files can also be rendered programmatically using the quarto command line:
quarto render my_quarto_file.qmd
or with the quartoR package:
quarto::render("my_quarto_file.qmd")
Anatomy of a qmd file
It contains three types of content:
An (optional) YAML header surrounded by —s.
Text mixed with simple text formatting like ## heading, bolds and italics.
Chunks of R code surrounded by ```{r}.
---title:"Hello, Penguins"format: htmlexecute:echo: false---## Meet the penguinsThe __penguins__ data contains size measurements for penguins from three islands in the Palmer Archipelago, Antarctica.The _three_ species of penguins have quite distinct distributions of physical dimensions (@fig-penguins).#| label: fig-penguins#| fig-cap: "Dimensions of penguins across three species."#| warning: falselibrary(tidyverse, quietly = TRUE)library(palmerpenguins)penguins|>ggplot(aes(x = flipper_length_mm, y = bill_length_mm))+geom_point(aes(color = species))+scale_color_manual(values = c("darkorange","purple", "cyan4"))+theme_minimal()
YAML header
YAML header
The YAML header is demarcated by three dashes (—) on either end. It informs on some documents meta-data and sets up many generic and output format specific options. The YAML consists of key: values pairs. The colon and space are required.
The YAML header is demarcated by three dashes (—) on either end. It informs on some documents meta-data and sets up many generic and output format specific options. The YAML consists of key: values pairs. The colon and space are required.
---title:"Toward a Unified Theory of High-Energy Metaphysics: Silly String Theory"date: 2008-02-29author:- name: Josiah Carberryid: jcorcid: 0000-0002-1825-0097email: josiah@psychoceramics.orgaffiliation:- name: Brown Universitycity: Providencestate: RIurl: www.brown.eduabstract:>The characteristic theme of the works of Stone is the bridge between culture and society. ...keywords:- Metaphysics- String Theorylicense:"CC BY"copyright:holder: Josiah Carberryyear: 2008citation:container-title: Journal of Psychoceramicsvolume: 1issue: 1doi: 10.5555/12345678funding:"The author received no specific funding for this work."---
YAML header
YAML headers can operate at the document level to manage execute options:
---title:"Hello, Penguins"subtitle:"Penguins are vertebrates"execute:echo: falseeval: truewarning: falseerror: true---
Or can set format specific options (here for html output):
---title:"Hello, Penguins"subtitle:"Penguins are vertebrates"format:html:theme: unitedcode-fold: truecode-summary:"see the code"execute:echo: trueeval: truewarning: falseerror: true---
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:
Markdown
Markdown
Quarto is based on Pandoc and uses its variation of markdown as its underlying document syntax. Pandoc markdown is an extended and slightly revised version of John Gruber’s Markdown syntax.
Markdown is a plain text format that is designed to be easy to write, and, even more importantly, easy to read:
A Markdown-formatted document should be publishable as-is, as plain text, without looking like it’s been marked up with tags or formatting instructions. – John Gruber
Rstudio’s visual editor toolbar includes buttons for the most commonly used formatting commands:
Markdown
Additional commands are available on the Format, Insert, and Table menus:
Format
Insert
Table
Markdown
Rstudio’s visual editor toolbar includes buttons for the most commonly used formatting commands:
Check out the Quarto official documentation to learn more about visual markdown editing:
Technical Writing covers features commonly used in scientific and technical writing, including citations, cross-references, footnotes, equations, embedded code, and LaTeX.
Content Editing provides more depth on visual editor support for tables, lists, pandoc attributes, CSS styles, comments, symbols/emojis, etc.
Shortcuts & Options documents the two types of shortcuts you can use with the editor: standard keyboard shortcuts and markdown shortcuts and describes various options for configuring the editor.
Markdown Output describes how the visual editor parses and writes markdown and describes various ways you can customize this.
Code blocks that use braces around the language name (e.g. ```{r}) are executable, and will be run by Quarto during render. Chunk options (optional), in YAML style, identified by #| at the beginning of the line are used to set chunk-specific meta-data and behaviours.
Going back to the penguins example:
---title:"Hello, Penguins"format: htmlexecute:echo: false---## Meet the penguinsThe __penguins__ data contains size measurements for penguins from three islands in the Palmer Archipelago, Antarctica.The _three_ species of penguins have quite distinct distributions of physical dimensions (@fig-penguins).````{r}#| label: fig-penguins#| fig-cap: "Dimensions of penguins across three species."#| warning: falselibrary(tidyverse, quietly = TRUE)library(palmerpenguins)penguins|>ggplot(aes(x = flipper_length_mm, y = bill_length_mm))+geom_point(aes(color = species))+scale_color_manual(values = c("darkorange","purple", "cyan4"))+theme_minimal()````
Computations (using R)
Code blocks that use braces around the language name (e.g. ```{r}) are executable, and will be run by Quarto during render. Chunk options (optional), in YAML style, identified by #| at the beginning of the line are used to set chunk-specific meta-data and behaviours.
Going back to the penguins example:
The _three_ species of penguins have quite distinct distributions of physical dimensions (@fig-penguins).````{r}#| label: fig-penguins#| fig-cap: "Dimensions of penguins across three species."#| warning: falselibrary(tidyverse, quietly = TRUE)library(palmerpenguins)penguins|>ggplot(aes(x = flipper_length_mm, y = bill_length_mm))+geom_point(aes(color = species))+scale_color_manual(values = c("darkorange","purple", "cyan4"))+theme_minimal()````
A chunk label (“fig-penguins”) has been defined, it allows referencing (and auto-numbering) the plot produced by the code as a figure anywhere in the text (@fig-penguins).
A figure caption has been defined and is used in the rendered output.
Warnings have been disabled and are discarded from the rendered output.
Computations (using R)
In addition to rendering the complete document to view the results of code chunks you can also run each code chunk interactively in the RStudio editor by clicking the icon or keyboard shortcut (Cmd/Ctrl + Shift + Enter).
RStudio executes the code and displays the results either inline within your file or in the Console, depending on your preference.
Quarto cross references provide automatic numbering and reference creation for figures, tables, equations, sections, listings, theorems, and proofs. In books, cross references work the same way except they can reach across chapters.
You can cross reference almost everything : figures, tables, equations, sections, …
Cross reference identifiers
To reference an item later we need an identifier for it.
Figures: To create a cross-referenceable figure and then refer to it:
![Elephant](elephant.png){#fig-elephant}See @fig-elephant for an illustration.
Or, using divs :
::: {#fig-elephant}![](elephant.png)An Elephant:::See @fig-elephant for an illustration.
Useful for sub-figures:
::: {#fig-elephants layout-ncol=2}![Surus](surus.png){#fig-surus}![Hanno](hanno.png){#fig-hanno}Famous Elephants:::See @fig-elephants for examples. In particular, @fig-hanno.
Cross references (non computational content)
Tables
For markdown tables, add a caption below the table, then include a #tbl- label in braces at the end of the caption. For example:
|Col1|Col2|Col3||------|------|------||A|B|C||E|F|G||A|G|G|: My Caption {#tbl-letters}See @tbl-letters.
::: {#tbl-panel layout-ncol=2}|Col1|Col2|Col3||------|------|------||A|B|C||E|F|G||A|G|G|: First Table {#tbl-first}|Col1|Col2|Col3||------|------|------||A|B|C||E|F|G||A|G|G|: Second Table {#tbl-second}Main Caption:::See @tbl-panel for details, especially @tbl-second.
Cross references (non computational content)
Equations
Provide an #eq- label immediately after an equation to make it referenceable. For example:
Black-Scholes(@eq-black-scholes)is a mathematical model that seeks to explain the behavior of financial derivatives, most commonly options:$$\frac{\partial\mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2}\frac{\partial^{2}\mathrm C}{\partial \mathrm S^2}+\mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =\mathrm r \mathrm C $$ {#eq-black-scholes}
Black-Scholes (Equation 1) is a mathematical model that seeks to explain the behavior of financial derivatives, most commonly options:
\[
\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2}
\frac{\partial^{2} \mathrm C}{\partial \mathrm S^2}
+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
\mathrm r \mathrm C
\qquad(1)\]
Cross references (computations)
Figures:
#| label: fig-plot#| fig-cap: "Plot"plot(cars)For example, see @fig-plot.
Quarto uses Pandoc to automatically format in text citations and create a reference list properly styled. You’ll need:
A quarto document formatted with citations (see next slide).
A bibliographic data source, for example a BibLaTeX (.bib) or BibTeX (.bibtex) file. This can be automatically generated when using the visual Quarto editor.
Optionally, a CSL file which specifies the formatting to use when generating the citations and bibliography.
Bibliography Files
Quarto supports bibliography files in a wide variety of formats including BibLaTeX and CSL. Add a bibliography to your document using the bibliography YAML metadata field. For example:
---title:"Oouh là là y'a des accents dans le titre"bibliography: references.bib---
Citations
Quarto uses the standard Pandoc markdown representation for citations (e.g. [@citation]) — citations go inside square brackets and are separated by semicolons. Each citation must have a key, composed of ‘@’ + the citation identifier from the database.
Here are some examples:
Markdown Format
Output (default)
Output(csl: diabetologia.csl) |
Blah Blah [see @knuth1984, pp. 33-35;
also @wickham2015, chap. 1]
Blah Blah [see @knuth1984, pp. 33-35; also @wickham2015, chap. 1]
Blah Blah see [1], pp. 33-35; also [1], chap. 1
Blah Blah [@knuth1984, pp. 33-35,
38-39 and passim]
Blah Blah [@knuth1984, pp. 33-35, 38-39 and passim]
Blah Blah [1], pp. 33-35, 38-39 and passim
Blah Blah [@wickham2015; @knuth1984].
Blah Blah [@wickham2015; @knuth1984].
Blah Blah [1, 2].
Wickham says blah [-@wickham2015]
Wickham says blah [-@wickham2015]
Wickham says blah [1]
You can also write in-text citations, as follows:
Markdown Format
Output (author-date format)
Output (numerical format)
@knuth1984 says blah.
@knuth1984 says blah.
[1] says blah.
@knuth1984 [p. 33] says blah.
@knuth1984 [p. 33] says blah.
[1] [p. 33] says blah.
Citations
Visual editor
Visual mode uses the standard Pandoc markdown representation for citations (e.g. [@citation]). Citations can be inserted from a variety of sources:
Your document bibliography. (bibliography: references.bib)
If you insert citations from Zotero, DOI look-up, or a search then they are automatically added to your document bibliography.
Citations
Visual editor
Use the Insert > Citation or the ctrl + shift + F8 keyboard shortcut to show the Insert Citation dialog:
Note that you can insert multiple citations by using the add button on the right side of the item display.
Quarto projects
When projects are larger than a simple analysis (e.g. a paper with additional analyses presented in supplementary material), it is useful to split the project reporting in several Quarto documents.
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.
In addition, projects can have special “types” that introduce additional behavior (e.g. websites, books or manuscripts).
Quarto projects
Project Metadata
All Quarto projects include a _quarto.yml configuration file. Any document rendered within the project directory will automatically inherit the metadata defined at the project level.
The configuration file contains both global options that apply to all formats (e.g. toc and bibliography) as well as format-specific options:
Quarto manuscripts is a recent format. To create a Quarto Manuscript you will have to clone a github template repository.
Quarto manuscript projects are made for writing and publishing scholarly articles.
A Quarto manuscript lets you:
Use one or more notebooks or .qmd documents as the source of content and computations, and then publish these computations alongside the manuscript, allowing readers to dive into your code.
Produce manuscripts in multiple formats (including LaTeX or MS Word formats required by journals), and give readers easy access to all of the formats through a manuscript website.
The output of a Quarto manuscript is a website (live example).
The article itself appears as the content of the website, and can include all the elements common to scholarly writing like figures, tables, equations, cross references and citations.
The website also makes available other formats (e.g. PDF, Docx) as well as links to all of the computations used to create the article.