Day 4 Activity
Activity: Your First Quarto Report
You’ll build a short Quarto report from scratch. Take 20 minutes for this activity.
Setup:
- Open RStudio.
File > New File > Quarto Document...- Title: “Penguin Exploration”, Author: your name.
- Pick HTML as the format.
- Uncheck “Use visual markdown editor” if you want to see the raw source.
- Click Create.
You’ll get a template document, potentially with example content. Delete anything below the YAML header if present. Keep the YAML.
Step 1 - Save the file
File > Save As...: call it penguin-report.qmd, save somewhere you’ll know where to find it.
Step 2 - Save the provided image
Place your downloaded fig-mass-histogram-1.png in the same folder where you saved penguin-report.qmd.
Step 3 - Upgrade your YAML
We can make the HTML output look professional right away. Modify the top of your document (between the --- lines) to add a Table of Contents (toc) and a custom theme. Be careful with indentation (use spaces instead of tabs) since YAML can be picky.
---
title: "Penguin Exploration"
author: "Your Name"
format:
html:
toc: TRUE
theme: litera
embed-resources: TRUE
---Be sure to include embed-resources: TRUE to ensure Quarto packages the entire document (CSS styles, JavaScript libraries, fonts, images) into one, self-contained HTML file. This enables offline viewing of the document and ensures that all content you create will be saved in the html file you submit on Canvas.
DON’T FORGET: Render and open your html file to see if it worked.
Step 4 - Add a setup chunk
Insert a code chunk (Ctrl + Alt + I on Windows or `Cmd + Option + I on Mac). Make it look like this:
```{r}
#| label: setup
#| include: FALSE
library(tidyverse)
library(palmerpenguins)
```DON’T FORGET: Render and open your html file to see if it worked.
Step 5 - Write an intro with a link, an image, and inline code
Below the setup chunk, add a heading and a paragraph.
Notice three new Markdown features here:
- Images: Formatted as
. We’re pulling the package hex logo straight from the web and using Quarto’s{width}andfig-alignattributes to float it to the right. - Hyperlinks: Formatted as
[text](url) - Inline code: The backticks around
`r nrow(penguins)`will automatically calculate the number of rows in the dataset when you render.
Copy and paste this into your document:
# Introduction
){width="120" fig-align="right"}
This report explores the [**Palmer Penguins**](https://allisonhorst.github.io/palmerpenguins/)
data set, which contains measurements on 344 penguins from
three islands in Antarctica.DON’T FORGET: Render and open your html file to see if it worked.
Step 6 - Add a plot
Copy and paste the code below to insert appropriate headings and another chunk (see if you can remember the keyboard shortcut for adding a chunk.) Then put a histogram in your new chunk. We are adding a fig-cap to label the figure, which lets us automatically reference it in our text.
# Exploratory Data Analysis
## Basic Histogram
{#fig-mass-histogram width=672}DON’T FORGET: Render and open your html file to see if it worked.
Step 7 - Add commentary
Below the plot, write 2-3 sentences (in Markdown) describing what you see. Include this exact text somewhere in your paragraph to see how Quarto handles cross-references:
As shown in @fig-mass-histogram, the data is...DON’T FORGET: Render and open your html file to see if it worked.
Step 8 - Add a table
Copy and paste the code below to add a new section heading and a code chunk that uses the kable() function from the knitr package to draw a clean table of the first 5 rows:
## Data Preview
::: {#tbl-preview .cell tbl-cap='First 5 rows of the penguins dataset'}
::: {.cell-output-display}
|species |island | bill_length_mm| bill_depth_mm| flipper_length_mm| body_mass_g|sex | year|
|:-------|:---------|--------------:|-------------:|-----------------:|-----------:|:------|----:|
|Adelie |Torgersen | 39.1| 18.7| 181| 3750|male | 2007|
|Adelie |Torgersen | 39.5| 17.4| 186| 3800|female | 2007|
|Adelie |Torgersen | 40.3| 18.0| 195| 3250|female | 2007|
|Adelie |Torgersen | NA| NA| NA| NA|NA | 2007|
|Adelie |Torgersen | 36.7| 19.3| 193| 3450|female | 2007|
:::
:::DON’T FORGET: Render and open your html file to see if it worked.
Step 9 - Final render
Press Cmd/Ctrl + Shift + K. Look at the HTML file that pops up. Notice your table of contents, your styled theme, the evaluated inline code, and the figure numbering.
If you finish early, try one or more of these:
- Create a Tabset: Wrap two heading and code chunks in a tabset block by copying and pasting the code below to hide multiple plots behind clickable tabs:
::: {.panel-tabset}
### Mass Plot
(Put your mass plot code chunk here)
### Flipper Plot
(Put a new flipper_length_mm plot code chunk here)
:::- Change the Output: Try changing
format: htmltoformat: docxin the YAML and re-render to get a Word document. (Note: themes and tabsets are HTML-only). - Hide the Code: Set
echo: FALSEin your YAML underexecute:to hide all code from the output, leaving only the text, plots, and tables.
---
title: "Penguin Exploration"
author: "Your Name"
format:
html:
toc: true
theme: litera
execute:
echo: false
---To turn in: Save your .qmd file and the rendered HTML. We’ll check back in at the end of class.