What is the #| anyway?
In Quarto, that is used to give information regarding the R chunk, and tells Quarto that anything on this line is not R code to run.
---
title: "Untitled"
format: html
editor: visual
---
---. Besides being at the top of the document, this is Quartoβs way of knowing that this specific part of the document is the YAML.
# to denote otherwise (as a reminder, in R code, a # comments out whatever is on that line, whether code or text). That is different in Quarto.
* to the beginning and end of your text (whether a single letter, word, sentence, paragraph, etc.).
*It will look like this.*
** to the beginning and end of your text (whether a single letter, word, sentence, paragraph, etc.).
**It will look like this.**
- and then a space between the text is needed. For a numbered list, you need the number, then a period, then a space before your text. Below are examples of both.
- Text
- More text
1. Text
2. More text
--- to enclose whatever was inside, for R chunks, Quarto uses "```". Whatever is inside an R chunk is treated as R code.
{}, and inside of that bracket is the letter "r". This tells Quarto to run the chunk as R code.
#| (called the hash-pipe) and then the label:.
#| anyway?#| label: r-is-awesome
- in between your words. In each Quarto document, there are two major types of R chunks.
library(tidyverse)
library(palmerpenguins)
penguins_new <- penguins |>
select(species, island, bill_length_mm, body_mass_g) |>
filter(species == "Adelie") |>
mutate(size_category = if_else(body_mass_g >= 3500, "Big","Small")) |>
arrange(desc(body_mass_g)) |>
rename(penguin_types = species)
sqrt(25)
[1] 5
sqrt(25) code as well as the answer (5).
#| label:. The #| lets R know that the information is related to the chunk itself and is not R code, while label: allows you to name the R chunk. You can name it whatever you want, for example:
#| label: first-R-chunk
tbl-
fig-
penguins_new dataset. Before in section SubsubsectionΒ 10.4.4.1, we "created" it. Now, letβs see what happens when we call the output.
penguins_new
# A tibble: 152 Γ 5 penguin_types island bill_length_mm body_mass_g size_category <fct> <fct> <dbl> <int> <chr> 1 Adelie Biscoe 43.2 4775 Big 2 Adelie Biscoe 41 4725 Big 3 Adelie Torgersen 42.9 4700 Big 4 Adelie Torgersen 39.2 4675 Big 5 Adelie Dream 39.8 4650 Big 6 Adelie Dream 39.6 4600 Big 7 Adelie Biscoe 45.6 4600 Big 8 Adelie Torgersen 42.5 4500 Big 9 Adelie Dream 37.5 4475 Big 10 Adelie Torgersen 41.8 4450 Big # βΉ 142 more rows
penguins_new, the Quarto document now displays the output, which in this case is penguins_new.
tbl-.
#| label: tbl-penguins_new_display
#| tbl-cap: "Showing what a plain table looks like in a Quarto document."
penguins_new
# A tibble: 152 Γ 5 penguin_types island bill_length_mm body_mass_g size_category <fct> <fct> <dbl> <int> <chr> 1 Adelie Biscoe 43.2 4775 Big 2 Adelie Biscoe 41 4725 Big 3 Adelie Torgersen 42.9 4700 Big 4 Adelie Torgersen 39.2 4675 Big 5 Adelie Dream 39.8 4650 Big 6 Adelie Dream 39.6 4600 Big 7 Adelie Biscoe 45.6 4600 Big 8 Adelie Torgersen 42.5 4500 Big 9 Adelie Dream 37.5 4475 Big 10 Adelie Torgersen 41.8 4450 Big # βΉ 142 more rows
knitr package ([D.1.13]), we can display tables with the kable() function, which is purposely dedicated to displaying tabular data in Quarto.
kable().kable() function to format tables for display in Quarto. This improves readability and ensures tables render cleanly in the final document.
kable() is not required when working interactively in R. The underlying objects (data frames, matrices, model summaries) can always be printed directly. We use kable() here purely for presentation purposes.
kableExtra package.
#| label: tbl-penguins_new_display_kable
library(knitr)
kable(head(penguins_new), caption = "This is an example of displaying a table using the kable command. On top of the data looking nicer than just a regular output, we can add a caption (and manipulate other aesthetic pieces.")
Table: This is an example of displaying a table using the kable command. On top of the data looking nicer than just a regular output, we can add a caption (and manipulate other aesthetic pieces. |penguin_types |island | bill_length_mm| body_mass_g|size_category | |:-------------|:---------|--------------:|-----------:|:-------------| |Adelie |Biscoe | 43.2| 4775|Big | |Adelie |Biscoe | 41.0| 4725|Big | |Adelie |Torgersen | 42.9| 4700|Big | |Adelie |Torgersen | 39.2| 4675|Big | |Adelie |Dream | 39.8| 4650|Big | |Adelie |Dream | 39.6| 4600|Big |
penguins_new vs the output from the kable() function, there is a stark difference. One of the most important differences is that you can add a caption that can illuminate the table more than just a simple output.
#| tbl-cap: if you were not using kable(). However, it is best practice to use kable().
penguins_new and not just the head() of penguins_new, so why did we do this?
head() then kable() would have produced a table displaying all rows from penguins_new, which would have the user scrolling for a long time. It is always your call as the creator of the document, but keep this in mind:
head(), your audience still gets a good picture of what your data looks like.
fig-.
fig-cap: on a line with the #|.
#| label: fig-example-ggplot2
#| fig-cap: "An example of a visualization made using ggplot2."
library(ggplot2)
ggplot(data = mpg, aes(x = cty, y = hwy)) +
geom_point()

cty) and highway mileage (hwy) from the mpg dataset.fig-cap:.
# symbol to set up sections. Here is an example:
# Section 1
# Section 2
## Section 2.1
# in the header. Each additional # means one level lower.
{} on the same line as the header, add a # within the {} followed up whatever name you want. Just how tables and graphs have specific prefixes, so do section headers, which is sec-. An example of a section name is as follows:
{#sec-creating-sections}
@fig-example-ggplot2 in my text, Quarto automatically turned it into a clickable link, with the @ symbol being the driving factor. It even indexes the figures based on the order theyβre created. If I were to add another graph earlier in the chapter, Quarto would automatically update the numbering so this one becomes "Figure 2" without me having to change a thing.