| continent | mean_life_exp | mean_gdp |
|---|---|---|
| Africa | 54.80604 | 3089.033 |
| Americas | 73.60812 | 11003.032 |
| Asia | 70.72848 | 12473.027 |
| Europe | 77.64860 | 25054.482 |
| Oceania | 80.71950 | 29810.188 |
Day 3 - Introduction to Data Analysis with R
Freie Universität Berlin - Theoretical Ecology
March 16, 2026
So far, you’ve been writing .R scripts.
This is great for running code, but what if you want to
If you have to repeat the analysis
Quarto lets you combine code, text, and output in one document.
Advantages:
Quarto is an open-source scientific and technical publishing system
Today we focus on documents.
Create a .qmd document
Write text and code into the document
Render the document to an output format (e.g. HTML)
.qmd documentIn the Files pane, click New File -> Quarto Document
Give it a name (e.g. my_report.qmd) and it opens as an empty file in RStudio.
A .qmd file has three types of content:
Ctrl + Shift + KRStudio will run all the code, combine it with your text, and produce the output document.
.qmd documentMarkdown text, Code, YAML header
Markdown is a simple markup language to create formatted text.
The basics
**text** becomes text*text* becomes textLists
Links
[Quarto website](https://quarto.org) becomes Quarto website
You don’t need to memorize all of this. Here is a quick reference.
Code chunks start and end with 3 backticks and contain R code:
Insert a code chunk
Code -> Insert chunkCtrl + Alt + I / Cmd + Option + IRun a code chunk
Code chunks have special comments starting with #| that control the output:
```{r}
#| echo: false
#| warning: false
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point()
```echo: true/false — Show the code in the output?eval: true/false — Run the code?warning: true/false — Show warnings?message: true/false — Show messages?You can also include R code inside text using inline code:
becomes:
The gapminder data contains 1704 observations.
This is powerful because the number updates automatically when the data changes.
The YAML header is at the top of the document between --- markers:
This sets the title, author, date, and output format.
You can set default options for all code chunks:
---
title: "My analysis"
author: "Selina Baldauf"
format: html
execute:
warning: false
message: false
---These can be overwritten by individual chunk options.
Task (30 min)
Reproducible documents with Quarto
Find the task description here
Change the output format in the YAML header:
You might need to install LaTeX first. The most convenient is to use the tinytex package:
Add options under the format to customize your document:
toc: Add a table of contentsnumber-sections: Number the section headerscode-fold: Hide code behind a button (HTML only)Be careful with the indentation, YAML is sensitive to spaces.
Control how figures appear in the output:
```{r}
#| label: fig-life-exp
#| fig-cap: "Life expectancy vs. GDP per capita in 2007"
#| fig-align: center
#| out-width: "80%"
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent)) +
theme_minimal()
```fig-cap: Figure captionfig-align: left, center, or rightout-width: Width of the figure in the outputlabel: Must start with fig- for figuresYou can reference labeled figures in the text:
becomes:
As we can see in Figure 1, life expectancy increases with GDP.
kable()By default, tibbles print as plain text. Use knitr::kable() to render a nice table:
```{r}
gapminder |>
filter(year == 2007) |>
group_by(continent) |>
summarize(
mean_life_exp = mean(lifeExp),
mean_gdp = mean(gdpPercap)
) |>
knitr::kable()
```| continent | mean_life_exp | mean_gdp |
|---|---|---|
| Africa | 54.80604 | 3089.033 |
| Americas | 73.60812 | 11003.032 |
| Asia | 70.72848 | 12473.027 |
| Europe | 77.64860 | 25054.482 |
| Oceania | 80.71950 | 29810.188 |
RStudio has a visual editor that provides a word-like interface for editing .qmd files.
The visual editor makes it easy to:
Quarto can do much more:
Check out the Quarto website for guides, examples, and a gallery.
Selina Baldauf // Reproducible documents with Quarto