Skip to content

Commit 9b031eb

Browse files
committed
Fix nested code chunk display syntax and internal data loading
- Replace knitr '{{r}}' syntax with standard Markdown four-backticks for displaying code chunks. - Update the example setup chunk to load and clean raw data internally, avoiding dependencies on intermediate files from previous episodes.
1 parent 3dfb832 commit 9b031eb

1 file changed

Lines changed: 68 additions & 7 deletions

File tree

episodes/05-reproducible-reports.Rmd

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,49 @@ source: Rmd
2626
```{r, include=FALSE}
2727
source("files/download_data.R")
2828
library(tidyverse)
29-
books2 <- read_csv("data_output/books_reformatted.csv")
29+
# Read raw data and apply cleaning steps from previous episodes
30+
books2 <- read_csv("data/books.csv") %>%
31+
rename(
32+
title = X245.ab,
33+
author = X245.c,
34+
callnumber = CALL...BIBLIO.,
35+
isbn = ISN,
36+
pubyear = X008.Date.One,
37+
subCollection = BCODE1,
38+
format = BCODE2,
39+
location = LOCATION,
40+
tot_chkout = TOT.CHKOUT,
41+
loutdate = LOUTDATE,
42+
subject = SUBJECT,
43+
callnumber2 = CALL...ITEM.
44+
) %>%
45+
mutate(
46+
pubyear = as.integer(pubyear),
47+
subCollection = recode(subCollection,
48+
"-" = "general collection",
49+
u = "government documents",
50+
r = "reference",
51+
b = "k-12 materials",
52+
j = "juvenile",
53+
s = "special collections",
54+
c = "computer files",
55+
t = "theses",
56+
a = "archives",
57+
z = "reserves"
58+
),
59+
format = recode(format,
60+
a = "book",
61+
e = "serial",
62+
w = "microform",
63+
s = "e-gov doc",
64+
o = "map",
65+
n = "database",
66+
k = "cd-rom",
67+
m = "image",
68+
"5" = "kit/object",
69+
"4" = "online video"
70+
)
71+
)
3072
```
3173

3274
## Introduction to Reproducible Reporting
@@ -87,10 +129,12 @@ The white space is where you write your narrative. You use **Markdown** syntax t
87129

88130
Code chunks are where your R code lives. They start with ` ```{r} ` and end with ` ``` `.
89131

90-
```{{r}}
132+
````
133+
```{r}
91134
# This is a code chunk
92135
summary(cars)
93136
```
137+
````
94138

95139
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).
96140

@@ -99,15 +143,28 @@ You can insert a new chunk by clicking the **+C** button in the editor toolbar,
99143
Let's clean up the example file and create a report using our `books` data.
100144

101145
1. Delete everything in the file *below* the YAML header.
102-
2. Add a new **setup** code chunk to load our libraries and data. It is good practice to label this chunk `setup`.
146+
2. Add a new **setup** code chunk to load our libraries and prepare the data.
103147

104148
```{{r}}
105149
#| label: setup
106150
#| include: false
107151
108152
library(tidyverse)
109-
# Load the data we saved in the previous episode
110-
books2 <- read_csv("data_output/books_reformatted.csv")
153+
154+
# Load data and rename columns for clarity
155+
books2 <- read_csv("data/books.csv") %>%
156+
rename(
157+
subCollection = BCODE1,
158+
tot_chkout = TOT.CHKOUT,
159+
format = BCODE2
160+
) %>%
161+
mutate(
162+
subCollection = recode(subCollection,
163+
"-" = "general collection",
164+
j = "juvenile",
165+
b = "k-12 materials"
166+
)
167+
)
111168
```
112169

113170
::::::::::::::::::::::::::::::::::::::::: callout
@@ -132,7 +189,8 @@ We are analyzing items with more than 10 checkouts to understand circulation pat
132189

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

135-
```{{r}}
192+
````
193+
```{r}
136194
#| label: plot-high-usage
137195
#| echo: false
138196
@@ -153,6 +211,7 @@ ggplot(data = booksHighUsage,
153211
theme_bw() +
154212
theme(axis.text.x = element_text(angle = 45, hjust = 1))
155213
```
214+
````
156215

157216
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.
158217

@@ -188,14 +247,16 @@ Add this to your document:
188247
The table below shows the average checkouts for each item format.
189248
```
190249

191-
```{{r}}
250+
````
251+
```{r}
192252
#| label: summary-table
193253
194254
books2 %>%
195255
group_by(format) %>%
196256
summarize(mean_checkouts = mean(tot_chkout, na.rm = TRUE)) %>%
197257
arrange(desc(mean_checkouts))
198258
```
259+
````
199260

200261
Render the document to see the updated report.
201262

0 commit comments

Comments
 (0)