library(tidyverse)
1 What is Quarto
Quarto enables you to weave together content and executable code into a finished document. That document can be a Word Doc, a PDF, HTML or a Presentation. Quarto will output (or “render”) that document in a format of your choosing. You can include or exclude code from your rendered document, include hyperlinks, citations, footnotes and more.
What you’re looking at right now is a typical Quarto output. Regular text (with all of the potential formatting options) is typed in, along with code and code outputs. You have a lot of control over the look and feel of your document (so can include styles and branding of your choice.)
2 Outputs
Quarto lets you create finished documents that get rendered as:
- Word docs
- PDFs
- HTML files
- Webpages
- Power Point Presentations
- Books
- Dashboards
3 Code chunks
Here are a few examples of how code and code outputs can be rendered:
Here just the code is presented:
Here the code can be viewed by clicking on the arrow:
Show the code
%>%
chickwts group_by(feed) %>%
mutate(mean_by_feed = mean(weight)) %>%
ungroup() %>%
mutate(feed = fct_reorder(feed, mean_by_feed)) %>%
ggplot(aes(x = feed,
y = weight,
colour = feed)) +
coord_flip() +
geom_jitter(show.legend = F,
size = 4,
alpha = 0.2,
width = 0.05) +
geom_point(stat = "summary",
fun = "mean",
size = 8,
show.legend = F)+
geom_hline(aes(yintercept = mean(weight)),
colour = "gray70",
size = 0.9) +
geom_segment(aes(
x = feed,
y = mean(weight),
xend = feed,
yend = mean_by_feed),
size = 2,
show.legend = F) +
labs(title = "Weight of chickens by feed group",
x = "Feed",
y = "Weight of chickens") +
theme_minimal()+
theme(legend.position = "none") +
theme(plot.background = element_rect(fill = "#deebf4", color = "black"))+
theme(panel.backgroun = element_rect(fill = "#deebf4")) +
theme(plot.title = element_text(hjust = 0.5,
face = "bold",
size = rel(2)))
- 1
- We are calculating the average weight of all of the chicks for a particular feed and creating a new variable that assigns that average weight to each observation.
- 2
- This function is from the forcats package. We’re ordering the feed variable by the average weight calculated above.
- 3
- This flips the plot 90 degrees. From here on, the x-axis will be the vertical axis and the y-axis the horizontal.
- 4
- geom_jitter() creates a dot (or point) that is not exactly at the coordinates of the data. This is so that points don’t overlap.
- 5
- This code will create a point for each feed at the average weight of chickens using that feed. We’ve made it big (size = 8)
- 6
- This creates a horizontal line (the grey line) that intercepts with the y axis at the average weight for all of the chickens. Note that the axis has been flipped above with the coord_flip() function and so the horizontal line is actually vertical in this case.
- 7
- This code creates a line from the grey vertial line (mean weight for all chickens) to the colored dot (mean weight for the chickens getting that feed). A segmented line must have an x and y starting point and an x end and y end (finishing point)
- 8
- The coordinates of the beginning of each line (for each feed)
- 9
- The coordinates of the end of each line
- 10
- Add title and axis labels
- 11
- Neaten up the plot but defining theme elements.
And here the code is hidden:
The GDP and Pop Size of Europe | |||
---|---|---|---|
Country | GDP per capita | Pop size | Life expectance |
Switzerland | 6384293 | ||
Norway | 4031441 | ||
Netherlands | 13786798 | ||
Denmark | 4994187 | ||
Germany | 77547043 | ||
Iceland | 226978 | ||
Austria | 7583298 | ||
Sweden | 8220029 | ||
Belgium | 9725119 | ||
United Kingdom | 56087801 |
4 Links and reference
References, footnotes, bibliographies and links in your text
Quarto let’s you create in text hyperlinks to figures like this Figure 1, links to sections of your document like this link to “What is quarto” section above. And you can create footnotes1 like this2 or an entire biography that integrates with EndNote, PubMed etc. Here is a reference to a few papers (Martin 2023), (James 2023), (Jenkin 2023).
You might not want to interrupt your text with a chunk of code but instead might want to make reference to a data within the text of your document itself. For example, I might want to say that the average height of starwars
characters is 1.75 meters. In his case the number wasn’t typed in as text but instead a line of code was used to insert the calculated answer. This is useful if you are writing a report that references a changing dataset that updates regularly.
But that fact that you’re here means that you probably know what Quarto is and you’re keen to get stuck in and learn how to use it. So let’s get going.
5 Create a Quarto document
Later, we’ll learn about creating Quarto projects, Quarto books and more. For now we’ll start with the basics. In R Studio you’ll create a new quarto file.
Next you’ll need to provide a title, author (optional) and decide on what kind of output your quarto document will render. You can change this later if needed.
6 YAML is your friend
The first thing you’ll notice at the top of your document is your yaml (which stands for YAML Ain’t Markup Language). This is where you can define settings for the entire document like title, format, styles and more.
We’ll get into the detail of yaml in a future lesson.
7 Adding text
Now you can use the text editor like you would with any other word processor. While you enter the text, you may find it useful to keep in visual
mode (which is user friendly and doesn’t require any knowledge of Markdown. You may prefer source
mode for more control. We’ll definitely be using source
mode for some of the more sophisticated functions that we’ll look at in future lessons.
8 Adding a code chunk
To add code you can either use the Insert pull down menu or type /
which will bring up a list of insert options. This will create a code “chunk” into which you can enter code.
You’ll notice the {r}
at the beginning of the chunk. This tells quarto
what language is going to be used in the chunk. It also provides you with an opportunity to include chunk options
. For example, you might not what the code itself to appear in your final document. In that case you can include the option `echo = FALSE` and only the output will appear in your final document. We’ll take a closer look at chunk options in a future lesson.
9 Render your document
Finally, to create the output, all you need to do is click on the render
button and boom-shaka-laka, you’ll have your first quarto
generated document.
10 What next?
Well done. Now you’ve got the basics. In the next lesson we’ll learn about chunk options.