#assigns the value
myVariable<-"popcorn"
#prints to screen
myVariable[1] "popcorn"
Quarto is software that creates outputs (web pages, pdfs, dashboards, slides, books, etc.) allowing you to communicate your coding projects to your audience. Quarto works with:
R
Python
Julia
ObservableJS
Typically you do your work in one or more .qmd (Quarto Markdown) documents, and then you render your .qmd file(s) into your selected outputs. It’s generally a good idea to try something, render, evaluate the output, and repeat.
Go ahead and click the Render button above, so we can see the unedited output of this file.
Let’s look at the very top of this file. The section between the opening and closing --- is called the YAML header (YAML: Yet Another Markup Language). This section includes important metadata, and includes configuration options for your Quarto file. Both the title and author field values display at the top of the rendered document, and the format field determines the type of output.
Your Task:
Make the following changes to the YAML header:
change the title to “All about Penguins”
change the author to your name
change the format to pdf
Now save and Render. What is different?
Specific formats can also have their own configuration rules. All the possibilities for html are listed here. For now, edit the YAML header again, so the format section looks like this:
format:
html:
toc: trueRender your file. Notice that there is now a table of contents section in your document, containing anchor links to section headers. Also, notice the indentations in the YAML header. It is very picky about indentations and spaces, so referring to documentation is a good idea.
Quarto documents are typically a mix of formatted text, code, and outputs from code. R Markdown is what allows you to create the formatted text. For example, a hyperlink in R Markdown looks like this:
I love [Watzek Library](https:library.lclark.edu).
The above translates to:
I love Watzek Library.
In the Files window, there’s a file titled rmarkdown.pdf. This provides syntax and examples for various R Markdown outputs.
It’s important to note that the raw R Markdown code is visible in the “Source” editor. The purpose of the Visual editor is to basically create the R Markdown for the author. There can be occasions where the Visual editor doesn’t give you exactly what you want, so you may need to toggle to the Source editor to address an issue.
Your task:
Open the rmarkdown.pdf cheat sheet, and toggle to the Source editor in this document. Write some R Markdown code below to display the image penguins.jpg, and give it a caption “Baby Penguins”.
Just like in .Rmd files, Quarto documents have code chunks, where code is executed. Here’s a basic example:
#assigns the value
myVariable<-"popcorn"
#prints to screen
myVariable[1] "popcorn"
When the document is rendered, each code chunk is executed.
You can create a code chunk by clicking the green c icon, or in the source using this syntax:
By default, a rendered output will show both the code chunk, plus any output from running the code. You can change this behavior by using one or more of Quarto’s execution options. Take a look at the chunk below:
In the rendered document, the plot appears, but the code chunk does not.
One of Quarto’s aims is to support multiple languages. Below is a python code chunk, which will run as long as the reticulate R package is installed:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)apple
banana
cherry
Your Task:
Create a code chunk below that displays code in the rendered output, but does not execute the code in the rendered output. Feel free to write your own code, or use samples from here.
mass_flipper <- ggplot(data = penguins,
aes(x = flipper_length_mm,
y = body_mass_g)) +
geom_point(aes(color = species,
shape = species),
size = 3,
alpha = 0.8) +
scale_color_manual(values = c("darkorange","purple","cyan4")) +
labs(title = "Penguin size, Palmer Station LTER",
subtitle = "Flipper length and body mass for Adelie, Chinstrap and Gentoo Penguins",
x = "Flipper length (mm)",
y = "Body mass (g)",
color = "Penguin species",
shape = "Penguin species") +
theme(legend.position = c(0.2, 0.7),
plot.title.position = "plot",
plot.caption = element_text(hjust = 0, face= "italic"),
plot.caption.position = "plot")
mass_flipperWhen performing any kind of analysis in R, you’ll often want to rely on the use of packages. Typically the sequence of events for installing and using a package in R is:
install.packages("tidyverse")
library(tidyverse)
In Quarto, you don’t necessarily have to have install.packages in a code chunk in your Quarto document. As long as the package has already been installed in your environment (e.g. via the console), you should be fine.
The same isn’t true for the library function - if you’re using functions from an external package in your Quarto file, you must use the library function in a code chunk:
# the execution option above prevents the tidyverse conflicts warning from being rendered
library(tidyverse)
library(palmerpenguins)Quarto has a number of options for working with plots. You can add references to figures using this syntax:
Figure 1 displays the distribution of flipper lengths among the sampled penguin data.
ggplot(data=penguins, mapping=aes(x=flipper_length_mm))+
geom_histogram()+
labs(x="")Your Task:
Referencing the plot below, add a line of text that references the plot, and add a code chunk option to match it (this will result in the plot being referenced as “Figure 2”). Also provide a caption.
ggplot(data=penguins, mapping=aes(x=bill_length_mm, y=bill_depth_mm, color=species))+
geom_point()+
geom_smooth(method = "lm")+
labs(x="Bill Length (mm)",
y="Bill Depth (mm)",
title="Bill Depth vs. Bill Length by Species in Palmer Penguins")Inline code - there may be times when you want to have dynamic values included in your markdown text. You can do this by defining variables in code chunks, and then including them within markdown text:
minMass<-min(penguins$body_mass_g, na.rm=TRUE)
maxMassKg<-max(penguins$body_mass_g, na.rm=TRUE)/1000The lightest penguin is 2700 grams. The heaviest penguin is 6.3 kilograms.
Your Task:
Create a code chunk that defines a variable meanBillLength, assigning it the value of the average bill_length_mm from the penguins data set. The write a sentence below that incorporates the variable you created, and render the document. How did it go?
Tabs
Tabs can be a nice way to group items together. In Quarto, this is a case where working in the Source editor is a better option. The syntax for tabs is shown below:
::: panel-tabset
## Tab 1
tab one content
## Tab 2
tab 2 content
## Tab 3
tab 3 content
:::The tabs don’t appear in the visual editor, but will once the document is rendered. Render the document, and take a look:
tab one content
tab 2 content
tab 3 content
Your turn:
In the Source editor, create a block of 3 tabs. In each tab, include the code for creating a ggplot visualization found here.