install.packages("tidyverse")
install.packages("palmerpenguins")
install.packages("quarto")
4 Individual Documents
The easiest way to get started with Quarto is to create stand-alone documents. These are self-contained documents, incorporating formatted text and executable code in a single file.
To create a new document with Quarto, you can simply use the menu options in RStudio or MS Visual Code, or create a file with the extension .qmd
.
4.1 Creating a document with RStudio
Before you start, make sure you have installed the Quarto software on your machine. It is a standalone software program, which needs to be installed for the rest of the process to work (see the Section 1.4 section).
If you already have a recent version of RStudio installed, you will need to install the following packages for the example:
Now, in RStudio we create a new project choosing the Quarto project option, as it appears in Figure 4.1.

We can name our project directory as first-example
and click Create Project
.
As a result, a new project should appear open on the screen, with the appearance shown in Figure 4.2.

Specifically, in the upper left panel we can see that, by default, the Visual editor has been opened, which allows creating Quarto documents in a more intuitive way. However, to start getting familiar with the structure of a quarto document from the beginning, we will switch to the Source editor to view the source code, by clicking on the button shown in the figure Figure 4.3.

4.2 Document structure
The following example code presents a basic structure of an individual Quarto document.
---
title: "My first document"
author: John Doe
date: 2024-12-20
---
Here we have a line of formatted Markdown content.
```{r}
#| label: my-label
# This is a code chunk. You can place here executable
# code that will be run by the R interprenter. By default, the final
# document will automatically integrate both the input code and the
# output from its execution
a <- 1 + 1
```
*Additional* Markdown content after the code chunk.
The file content consists of two parts:
- Preamble: is delimited by two
---
tags. Within this area we can assign values to configuration options for layout and creating the document, such as title, author(s), date, etc.
We can also configure various options related to the output format of the documents.
- Document body: is composed of text paragraphs formatted using the Markdown markup syntax, which we will see later. In addition, executable code fragments or chunks can also be inserted into the text, which are marked up using a special syntax (as we see in the example above).
Each chunk of executable code is delimited as follows:
```{r}
# Código en R
```
Although in this workshop we focus on the R language, you should know that Quarto also supports other programming languages such as Python, Julia or Observable.
We can change the programming language of each code chunk by indicating its name at the beginning, for example:
```{python}
# Código en Python
```
Nevertheless, this code chunk in other languages requires additional configuration. For example, we must set up a Python interpreter to execute code chunks written in this language.
4.2.1 The preamble
A basic example of a preamble is as follows (although it would be sufficient to simply provide a title for the document):
---
title: "My first document"
author: John Doe
date: 2024-12-20
---
Of course, more options can be added, which we will explain next
4.2.2 List of options
There is an extensive list of configuration options that we can include in our documents.
HTML output options: these allow us to configure various basic aspects of the document, such as the title and subtitle, date, author (or list of authors), summary or DOI; formatting options such as the subject or advanced styles for HTML content with CSS; numbering and table of contents, etc.
PDF output options: these offer the possibility of configuring multiple parameters for the creation of the document in this format, many of them similar to those for the HTML output. A particularly relevant option is to choose the LaTeX document format (
documentclass
option), which defines the general appearance of the layout to be used. By default, classes from the KOMA Script metapackage are used, such asscrartcl
orscrbook
. It is also important to indicate thepapersize
option, in our case to ensure that a standard format is used such as A4. The citation format is also relevant, being able to choose, for example, the BibLaTeX engine which is more powerful, with multilingual support and for native UTF-8 character encoding. Finally, it is also important to indicate the compilation engine. If you want full flexibility in document layout, it is highly recommended to use the XeLaTeX engine (optionpdf-engine: xelatex
), which is the default used by Quarto.
4.2.3 Basic Markdown syntax
In the following link you can find a quick basic tutorial that shows the basic options of the Markdown syntax accepted in Quarto documents to format textual content.
4.3 Creating documents (output)
By default, if we do not indicate anything, Quarto will generate a single output format of the document in HTML. However, it is possible to define more than one output format by including more configuration options. Of course, you can indicate different options to generate several output formats simultaneously, or to choose the output format that we want to produce based on our interests, selecting the format that we need when previewing or when generating the final document.
4.3.1 Preview
To preview the document we have to press the Render button in the tools menu of the RStudio interface, as shown in Figure 4.4.
By default, our main web browser or a panel in the RStudio interface will open showing the HTML page with the document already generated. By clicking on the gear icon next to the Render button we can select, among other things, the type of preview we want to be launched after completing the creation of the document or completely disable said preview. Available options are shown in Figure 4.5

4.3.2 Selecting output type
When we have several output format options configured in our document, we can choose at preview time which of the formats is chosen to generate the document. In the Figure 4.6 you can see an example of a document that includes settings for two output formats (HTML and PDF) and the change in the Render button, in which a small black arrow now appears just to the right of the button icon to display the two available output options.

4.3.3 Basic configuration options
Below is an example of some basic configuration options that are typically found in documents formatted as HTML output.
---
title: "My first document"
author:
- "John Doe"
- "Mary Jane"
date: 2024-12-20
lang: en
bibliography: references.bib
format:
html:
theme: cosmo
toc: true
number-sections: true
html-math-method: katex
css: styles.css
pdf:
documentclass: scrreprt
---
REST OF THE DOCUMENT
In this example, in addition to the author and the date, a list of two authors is indicated, the main language of the document (Spanish), the bibliography reference file (in .bib
format) and, within the HTML options, the layout topic, the inclusion of a table of contents (located by default at the top right), section numbering, selection of the engine to render equations in the document and a custom styles file in CSS format to adjust some fine layout options.
One option worth highlighting is to force all resources (images, style information, etc.) to be integrated into the HTML file itself, to facilitate direct sharing or publication of the document without having to also provide the auxiliary files necessary to display it in the browser. This option is shown below:
format:
html:
embed-resources: true
4.4 Executable code chunks
The most distinctive feature of documents created with Quarto is the possibility of inserting executable code fragments, called chunks, into the document itself. This also includes the option for said code to generate different results (numeric, graphic, tables, animations, etc.) that are integrated directly into the document. In this way, if we keep the code updated, the correct versions of said results will always be generated.
The executable code fragments have the following structure:
```{r}
#| label: id-fragmento
# Aquí va el código ejecutable
a = c(1, 2, 3, 4)
b = a^2
```
The triplet of characters ```
is called a fence and delimits the beginning and end of the code fragment. Immediately after the opening delimiter, the identifier of the programming language in which the code of that fragment is written is written in curly braces. This information is used to choose the appropriate syntax highlighting to display the code of that language and to select the interpreter that executes the code and produces the results.
In the following lines we can include one or several configuration options specific to that code fragment, using the syntax #| option: value
. For example, in the previous fragment the option #| label: fragment-id
creates a label (which must be unique) to identify that fragment of code within the document.
Some frequently used options are:
eval: true | false | [...]
: Indicates whether the content of that snippet should be evaluated (executed). A list of positive or negative line numbers can be passed to explicitly select which lines of code are included (positive) or excluded (negative) from execution.echo: true | false | fenced | [...]
: Indicates whether the source code of the snippet should be included in the document or not. Thefenced
option also includes the cell delimiter as part of the output. Finally, it also accepts a list of positive or negative line numbers to select which lines of code will or will not be displayed in the snippet.output: true | false | asis
: To decide whether the result of the code execution is included in the document or not. Theasis
value forces the result to be treated as raw Markdown content.warning: true | false
: Indicates whether warning messages should be included in the output.error: true | false
: Indicates whether generated error messages are included in the output.message: true | false
: Indicates whether generated information messages are included in the output.
When fragments generate figures, these are inserted into the document itself. Let’s look at an example:
```{r}
#| label: fig-example-cars
#| fig-cap: "Gráfico de correlación lineal positiva entre el kilometraje en ciudad y en carretera de diferentes modelos de coches."
library(ggplot2)
#| label: scatterplot
#| echo: true
ggplot(mpg, aes(x = hwy, y = cty, color = cyl)) +
geom_point(alpha = 0.5, size = 2) +
scale_color_viridis_c() +
theme_minimal()
```

It is important that the fragment identifier we choose for the code that generates one or more figures begins with the prefix fig-
.
In this way, we ensure that Quarto automatically assigns a number to the generated figure and that we can create cross-references (internal links) to that figure in our document.
As we will see later, other output types such as tables also need to be assigned a specific pattern in their fragment identifier so that they are automatically numbered and can be referenced within the document.
Figure management in Quarto is quite sophisticated, to the point that you can easily organize several subfigures with their respective individual descriptions, as shown in the following example using some additional options.
```{r}
#| label: fig-mpg-subplot
#| fig-cap: "City and road mileage of 38 popular car models."
#|
#| fig-subcap:
#| - "Color by num. of cylinders."
#| - "Color by motor displacement."
#| layout-ncol: 1
ggplot(mpg, aes(x = hwy, y = cty, color = cyl)) +
geom_point(alpha = 0.5, size = 2) +
scale_color_viridis_c() +
theme_minimal()
ggplot(mpg, aes(x = hwy, y = cty, color = displ)) +
geom_point(alpha = 0.5, size = 2) +
scale_color_viridis_c(option = "E") +
theme_minimal()
```


Some common options for chunks that generate figures are:
fig-width
: Width of the figure.fig-height
: Height of the figure.fig-cap
: String in quotes to be inserted as a caption.fig-alt
: Alternative text message that fills thealt
attribute of the HTML image (for example, to improve the accessibility of the content).fig-dpi
: Resolution setting of the figure (in dots per inch).
The tutorial on executable code snippets in the official documentation presents more information and examples on how to use this powerful Quarto feature.