You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- 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
+
:::
25
21
26
22
27
23
28
24
## Introduction to Reproducible Reporting
29
25
30
-
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.
26
+
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.
31
27
32
28
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.
33
29
@@ -37,21 +33,19 @@ But what if the data changes next month? You would have to re-run your script, r
37
33
38
34
To create a new Quarto document in RStudio:
39
35
40
-
1. Click the **File** menu.
41
-
2. Select **New File** > **Quarto Document...**
42
-
3. In the dialog box, give your document a **Title** (e.g., "Library Usage Report") and enter your name as **Author**.
43
-
4. Ensure **HTML** is selected as the output format.
44
-
5. Click **Create**.
36
+
1.Click the **File** menu.
37
+
2.Select **New File**\>**Quarto Document...**
38
+
3.In the dialog box, give your document a **Title** (e.g., "Library Usage Report") and enter your name as **Author**.
39
+
4.Ensure **HTML** is selected as the output format.
40
+
5.Click **Create**.
45
41
46
42
RStudio will open a new file with some example content. Notice the file extension is `.qmd`.
47
43
48
-
::::::::::::::::::::::::::::::::::::::::: callout
49
-
44
+
::: callout
50
45
## Quarto vs. RMarkdown
51
46
52
47
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.
53
-
54
-
:::::::::::::::::::::::::::::::::::::::::
48
+
:::
55
49
56
50
## Anatomy of a Quarto Document
57
51
@@ -61,7 +55,7 @@ A Quarto document has three main parts:
61
55
62
56
At the very top, enclosed between two lines of `---`, is the **YAML Header**. This contains metadata about the document.
63
57
64
-
```yaml
58
+
```yaml
65
59
---
66
60
title: "Library Usage Report"
67
61
author: "Your Name"
@@ -71,19 +65,18 @@ format: html
71
65
72
66
### 2. Markdown Text
73
67
74
-
The white space is where you write your narrative. You use **Markdown** syntax to format text.
68
+
The white space is where you write your narrative. You use **Markdown** syntax to format text.
75
69
76
-
-`**Bold**` for **bold text**
77
-
-`*Italics*` for *italics*
78
-
-`# Heading 1` for a main title
79
-
-`## Heading 2` for a section title
80
-
-`- List item` for bullet points
70
+
-`**Bold**` for **bold text**
71
+
-`*Italics*` for *italics*
72
+
-`# Heading 1` for a main title
73
+
-`## Heading 2` for a section title
74
+
-`- List item` for bullet points
81
75
82
76
### 3. Code Chunks
83
77
84
-
Code chunks are where your R code lives. They start with ` ```{r} ` and end with ` ``` `.
78
+
Code chunks are where your R code lives. They start with ```` ```{r} ```` and end with ```` ``` ````.
85
79
86
-
````
87
80
88
81
```r
89
82
# This is a code chunk
@@ -99,7 +92,6 @@ summary(cars)
99
92
3rd Qu.:19.0 3rd Qu.: 56.00
100
93
Max. :25.0 Max. :120.00
101
94
```
102
-
````
103
95
104
96
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).
105
97
@@ -110,53 +102,27 @@ Let's clean up the example file and create a report using our `books` data.
110
102
1. Delete everything in the file *below* the YAML header.
111
103
2. Add a new **setup** code chunk to load our libraries and prepare the data.
112
104
113
-
```{{r}}
114
-
#| label: setup
115
-
#| include: false
116
-
117
-
library(tidyverse)
118
-
119
-
# Load data and rename columns for clarity
120
-
books2 <- read_csv("data/books.csv") %>%
121
-
rename(
122
-
subCollection = BCODE1,
123
-
tot_chkout = TOT.CHKOUT,
124
-
format = BCODE2
125
-
) %>%
126
-
mutate(
127
-
subCollection = recode(subCollection,
128
-
"-" = "general collection",
129
-
j = "juvenile",
130
-
b = "k-12 materials"
131
-
)
132
-
)
133
-
```
134
105
135
-
::::::::::::::::::::::::::::::::::::::::: callout
136
106
107
+
::: callout
137
108
## Chunk Options
138
109
139
-
Notice the lines starting with `#|`. These are **chunk options**.
140
-
-`#| label: setup` gives the chunk a name.
141
-
-`#| include: false` runs the code but hides the code and output from the final report. This is great for loading data silently.
142
-
143
-
:::::::::::::::::::::::::::::::::::::::::
110
+
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.
111
+
:::
144
112
145
113
### Adding Analysis
146
114
147
115
Now, let's add a section header and some text.
148
116
149
-
```markdown
117
+
```markdown
150
118
## High Usage Items
151
119
152
120
We are analyzing items with more than 10 checkouts to understand circulation patterns across sub-collections.
153
121
```
154
122
155
123
Next, insert a new code chunk and paste the plotting code we developed in the previous episode (ggplot2).
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.
162
128
@@ -165,82 +131,61 @@ Setting `#| echo: false` will display the *plot* in the report, but hide the R *
165
131
Now comes the magic. Click the **Render** button (blue arrow icon) at the top of the editor pane.
166
132
167
133
RStudio will:
134
+
168
135
1. Run all your code chunks from scratch.
169
136
2. Generate the plots and results.
170
137
3. Combine them with your text.
171
138
4. Create a new file named `library_usage_report.html` in your project folder.
172
139
5. Open a preview of the report.
173
140
174
-
::::::::::::::::::::::::::::::::::::::: challenge
175
-
141
+
:::: challenge
176
142
## Challenge: Add a Summary Table
177
143
178
144
1. Add a new header `## Summary Statistics` to your Quarto document.
179
145
2. Insert a new code chunk.
180
146
3. Write code to calculate the mean checkouts per format (Hint: use `group_by(format)` and `summarize()`).
181
147
4. Render the document again to see your new table included in the report.
182
148
183
-
::::::::::::::: solution
184
-
149
+
::: solution
185
150
## Solution
186
151
187
152
Add this to your document:
188
153
189
-
```markdown
154
+
```markdown
190
155
## Summary Statistics
191
156
192
157
The table below shows the average checkouts for each item format.
0 commit comments