Hello Quarto

Author

Greg Martin

1 What is Quarto

This page was created with quarto 🔥 In this tutorial you’ll learn how to create your first quarto document.

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:

library(tidyverse)

Here the code can be viewed by clicking on the arrow:

Note

Notice the annotations next to the code that pop up if you click on the numbers.

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.
Figure 1: Chickweight by feed.

And here the code is hidden:

Note

Click on the tab buttons to view the rendered out.

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.

Now you’re ready to start using quarto.

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.

First code chunk

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.

My first chunk option

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.

Render your first document

10 What next?

Well done. Now you’ve got the basics. In the next lesson we’ll learn about chunk options.

References

James, Andrew. 2023. “Deep Data.” Science 90: 1–23.
Jenkin, Pat. 2023. “Global Health Challenges.” Public Health Africa 13: 4–20.
Martin, Greg. 2023. “The Future of Coding.” Journal of Data Science 10: 1–20.

Footnotes

  1. This is my first footnote↩︎

  2. This is another footnote↩︎