Skip to content

Commit b5ac24a

Browse files
authored
Merge pull request #140 from LibraryCarpentry/Episode-5_formatting
2 parents 2f4bbe3 + 80b25cb commit b5ac24a

2 files changed

Lines changed: 51 additions & 70 deletions

File tree

episodes/05-reproducible-reports.Rmd

Lines changed: 50 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@ exercises: 15
55
source: Rmd
66
---
77

8-
::::::::::::::::::::::::::::::::::::::: objectives
9-
10-
- Describe the value of reproducible reporting.
11-
- Create a new Quarto document (`.qmd`) in RStudio.
12-
- Use Markdown syntax to format text.
13-
- Create and run code chunks within a Quarto document.
14-
- Render a Quarto document to an HTML report.
15-
16-
::::::::::::::::::::::::::::::::::::::::::::::::::
17-
18-
:::::::::::::::::::::::::::::::::::::::: questions
19-
20-
- How can I combine my code, results, and narrative into a single document?
21-
- How can I automatically update my reports when my data changes?
22-
- What is Quarto and how does it differ from a standard R script?
23-
24-
::::::::::::::::::::::::::::::::::::::::::::::::::
8+
::: objectives
9+
- Describe the value of reproducible reporting.
10+
- Create a new Quarto document (`.qmd`) in RStudio.
11+
- Use Markdown syntax to format text.
12+
- Create and run code chunks within a Quarto document.
13+
- Render a Quarto document to an HTML report.
14+
:::
15+
16+
::: questions
17+
- How can I combine my code, results, and narrative into a single document?
18+
- How can I automatically update my reports when my data changes?
19+
- What is Quarto and how does it differ from a standard R script?
20+
:::
2521

2622
```{r, include=FALSE}
2723
source("files/download_data.R")
@@ -73,7 +69,7 @@ books2 <- read_csv("data/books.csv") %>%
7369

7470
## Introduction to Reproducible Reporting
7571

76-
So far, we have been writing code in `.R` scripts. This is excellent for data analysis, but what happens when you need to share your findings with a colleague or a library director? You might copy a plot into a Word document or an email, then type out your interpretation.
72+
So far, we have been writing code in `.R` scripts. This is excellent for data analysis, but what happens when you need to share your findings with a colleague or a library director? You might copy a plot into a Word document or an email, then type out your interpretation.
7773

7874
But what if the data changes next month? You would have to re-run your script, re-save the plot, copy it back into Word, and update your text. This manual process is prone to errors and tedious.
7975

@@ -83,21 +79,19 @@ But what if the data changes next month? You would have to re-run your script, r
8379

8480
To create a new Quarto document in RStudio:
8581

86-
1. Click the **File** menu.
87-
2. Select **New File** > **Quarto Document...**
88-
3. In the dialog box, give your document a **Title** (e.g., "Library Usage Report") and enter your name as **Author**.
89-
4. Ensure **HTML** is selected as the output format.
90-
5. Click **Create**.
82+
1. Click the **File** menu.
83+
2. Select **New File** \> **Quarto Document...**
84+
3. In the dialog box, give your document a **Title** (e.g., "Library Usage Report") and enter your name as **Author**.
85+
4. Ensure **HTML** is selected as the output format.
86+
5. Click **Create**.
9187

9288
RStudio will open a new file with some example content. Notice the file extension is `.qmd`.
9389

94-
::::::::::::::::::::::::::::::::::::::::: callout
95-
90+
::: callout
9691
## Quarto vs. RMarkdown
9792

9893
If you have used R before, you might be familiar with RMarkdown (`.Rmd`). Quarto (`.qmd`) is the next-generation version of RMarkdown. It works very similarly but supports more languages (like Python and Julia) and has better features for scientific publishing.
99-
100-
:::::::::::::::::::::::::::::::::::::::::
94+
:::
10195

10296
## Anatomy of a Quarto Document
10397

@@ -107,7 +101,7 @@ A Quarto document has three main parts:
107101

108102
At the very top, enclosed between two lines of `---`, is the **YAML Header**. This contains metadata about the document.
109103

110-
```yaml
104+
``` yaml
111105
---
112106
title: "Library Usage Report"
113107
author: "Your Name"
@@ -117,24 +111,22 @@ format: html
117111

118112
### 2. Markdown Text
119113

120-
The white space is where you write your narrative. You use **Markdown** syntax to format text.
114+
The white space is where you write your narrative. You use **Markdown** syntax to format text.
121115

122-
- `**Bold**` for **bold text**
123-
- `*Italics*` for *italics*
124-
- `# Heading 1` for a main title
125-
- `## Heading 2` for a section title
126-
- `- List item` for bullet points
116+
- `**Bold**` for **bold text**
117+
- `*Italics*` for *italics*
118+
- `# Heading 1` for a main title
119+
- `## Heading 2` for a section title
120+
- `- List item` for bullet points
127121

128122
### 3. Code Chunks
129123

130-
Code chunks are where your R code lives. They start with ` ```{r} ` and end with ` ``` `.
124+
Code chunks are where your R code lives. They start with ```` ```{r} ```` and end with ```` ``` ````.
131125

132-
````
133126
```{r}
134127
# This is a code chunk
135128
summary(cars)
136129
```
137-
````
138130

139131
You can insert a new chunk by clicking the **+C** button in the editor toolbar, or by pressing <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>I</kbd> (Windows/Linux) or <kbd>Cmd</kbd>+<kbd>Option</kbd>+<kbd>I</kbd> (Mac).
140132

@@ -145,7 +137,7 @@ Let's clean up the example file and create a report using our `books` data.
145137
1. Delete everything in the file *below* the YAML header.
146138
2. Add a new **setup** code chunk to load our libraries and prepare the data.
147139

148-
```{{r}}
140+
```{r}
149141
#| label: setup
150142
#| include: false
151143
@@ -167,29 +159,24 @@ books2 <- read_csv("data/books.csv") %>%
167159
)
168160
```
169161

170-
::::::::::::::::::::::::::::::::::::::::: callout
171-
162+
::: callout
172163
## Chunk Options
173164

174-
Notice the lines starting with `#|`. These are **chunk options**.
175-
- `#| label: setup` gives the chunk a name.
176-
- `#| include: false` runs the code but hides the code and output from the final report. This is great for loading data silently.
177-
178-
:::::::::::::::::::::::::::::::::::::::::
165+
Notice the lines starting with `#|`. These are **chunk options**. - `#| label: setup` gives the chunk a name. - `#| include: false` runs the code but hides the code and output from the final report. This is great for loading data silently.
166+
:::
179167

180168
### Adding Analysis
181169

182170
Now, let's add a section header and some text.
183171

184-
```markdown
172+
``` markdown
185173
## High Usage Items
186174

187175
We are analyzing items with more than 10 checkouts to understand circulation patterns across sub-collections.
188176
```
189177

190178
Next, insert a new code chunk and paste the plotting code we developed in the previous episode (ggplot2).
191179

192-
````
193180
```{r}
194181
#| label: plot-high-usage
195182
#| echo: false
@@ -211,7 +198,6 @@ ggplot(data = booksHighUsage,
211198
theme_bw() +
212199
theme(axis.text.x = element_text(angle = 45, hjust = 1))
213200
```
214-
````
215201

216202
Setting `#| echo: false` will display the *plot* in the report, but hide the R *code* that generated it. This is often preferred for reports intended for non-coders.
217203

@@ -220,67 +206,61 @@ Setting `#| echo: false` will display the *plot* in the report, but hide the R *
220206
Now comes the magic. Click the **Render** button (blue arrow icon) at the top of the editor pane.
221207

222208
RStudio will:
209+
223210
1. Run all your code chunks from scratch.
224211
2. Generate the plots and results.
225212
3. Combine them with your text.
226213
4. Create a new file named `library_usage_report.html` in your project folder.
227214
5. Open a preview of the report.
228215

229-
::::::::::::::::::::::::::::::::::::::: challenge
230-
216+
:::: challenge
231217
## Challenge: Add a Summary Table
232218

233219
1. Add a new header `## Summary Statistics` to your Quarto document.
234220
2. Insert a new code chunk.
235221
3. Write code to calculate the mean checkouts per format (Hint: use `group_by(format)` and `summarize()`).
236222
4. Render the document again to see your new table included in the report.
237223

238-
::::::::::::::: solution
239-
224+
::: solution
240225
## Solution
241226

242227
Add this to your document:
243228

244-
```markdown
229+
``` markdown
245230
## Summary Statistics
246231

247232
The table below shows the average checkouts for each item format.
248233
```
249234

250-
````
251-
```{r}
235+
```{{r}}
252236
#| label: summary-table
253237
254238
books2 %>%
255239
group_by(format) %>%
256240
summarize(mean_checkouts = mean(tot_chkout, na.rm = TRUE)) %>%
257241
arrange(desc(mean_checkouts))
258242
```
259-
````
260243

261244
Render the document to see the updated report.
262-
263-
:::::::::::::::::::::::::
264-
265-
::::::::::::::::::::::::::::::::::::::::::::::::::
245+
:::
246+
::::
266247

267248
## Why This Matters
268249

269-
By using Quarto, your report is now **reproducible**.
250+
By using Quarto, your report is now **reproducible**.
270251

271252
If you download a new version of `books.csv` next month:
253+
272254
1. Save it to your `data/` folder.
273255
2. Open your Quarto document.
274256
3. Click **Render**.
275257

276258
Your report will automatically update with the new data, creating a fresh plot and table without you having to copy-paste a single thing.
277259

278-
:::::::::::::::::::::::::::::::::::::::: keypoints
279-
280-
- **Quarto** allows you to mix code and text to create reproducible reports.
281-
- Use the **YAML header** to configure document metadata like title and output format.
282-
- **Code chunks** run R code and can display or hide input/output using options like `#| echo: false`.
283-
- **Rendering** the document executes the code and produces the final output (HTML, PDF, etc.).
284-
- This workflow saves time and reduces errors when reporting on data that changes over time.
285-
286-
::::::::::::::::::::::::::::::::::::::::::::::::::
260+
::: keypoints
261+
- **Quarto** allows you to mix code and text to create reproducible reports.
262+
- Use the **YAML header** to configure document metadata like title and output format.
263+
- **Code chunks** run R code and can display or hide input/output using options like `#| echo: false`.
264+
- **Rendering** the document executes the code and produces the final output (HTML, PDF, etc.).
265+
- This workflow saves time and reduces errors when reporting on data that changes over time.
266+
:::

lc-r.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: 0c1aa31e-0b52-4784-994c-ceebf424a714
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

0 commit comments

Comments
 (0)