Skip to content

Commit 86f9813

Browse files
committed
Rework knitr demo for smoother flow
1 parent a4bf121 commit 86f9813

File tree

1 file changed

+69
-74
lines changed

1 file changed

+69
-74
lines changed

materials/knitr.md

+69-74
Original file line numberDiff line numberDiff line change
@@ -14,104 +14,109 @@ language: R
1414

1515
### Getting started
1616

17-
* Open RStudio
17+
* `install.packages("rmarkdown")` in console for permanence
1818
* `File` -> `New File` -> `R Markdown`
1919
* Enter a title and author(s).
2020
* Choose 'Default Output Format' as `HTML`.
2121
* Generates a basic stub of a `.Rmd` document
22-
* Customize "front matter" at the top of the document between the `---`.
23-
* Press `Knit` to create an `HTML` from the document
24-
* 1st time may ask you to install some packages
25-
* `install.packages("rmarkdown")` in console for permanence
26-
* Runs the code in the code chunks and prints their output along with the
27-
markdown formatted text
28-
* Can also create `PDF` & Word versions of our files
29-
* `PDF` Requires `pandoc` and `TeX` installation
30-
* Use the `Knit` dropdown or change `output: pdf_document`
31-
* R Notebook
32-
* `output: html_notebook`
33-
* "Interactive R Markdown"
34-
* execute individual code chunks
35-
* output in editor pane
22+
* Customize "front matter" at the top of the document between the `---`.
23+
* Delete everything below the second `---`
3624

3725
### Markdown
3826

3927
* Basic approach to formatting text
4028
* Let's you do
29+
* `# Headers`
4130
* `*italics*`
4231
* `**bold**`
4332
* `[links](http://google.com)`
4433
* Lists
4534
* `*`
4635
* `1.`
47-
* `# Header`
36+
37+
38+
<pre><code>
39+
## Concept
40+
41+
Exploration of population dynamic patterns at **The Portal Project**.
42+
How do counts of rodents like *Dipodomys* species change through time?
43+
44+
In this document I will:
45+
46+
1. Load data from the [Portal Project Teaching Database](http://figshare.com/articles/Portal_Project_Teaching_Database/1314459)
47+
2. Process it into population time series
48+
3. And make initial visualizations
49+
</code></pre>
50+
4851
* Easy to read for humans
4952
* Easy to convert into other things for computers
50-
* Common on lots of websites
53+
54+
* Press `Knit` to create `HTML` from the document
55+
* Can also create `PDF` & Word versions of our files
56+
* `PDF` Requires `pandoc` and `TeX` installation
57+
* Use the `Knit` dropdown or change `output: pdf_document`
58+
59+
* Markdown is common on lots of websites
5160
* Used to create all of the exercises and lectures in this course
5261
* Github will automatically render it
5362
* [https://github.com/ethanwhite/CV/blob/master/CV.md](https://github.com/ethanwhite/CV/blob/master/CV.md)
5463

55-
<pre><code>Explore patterns in population dynamics at Portal.
64+
### R chunks
5665

57-
## Required Libraries</code></pre>
66+
* R Markdown allows you to include code to run in the document
67+
* Click on `Insert` and choose R
5868

59-
### R chunks
69+
<pre><code>
70+
## Required Packages
6071

61-
* Set R code inside a set of <code>```</code> with the `{r}` designation
72+
```{r}
73+
library(dplyr)
74+
library(ggplot2)
75+
```
76+
</code></pre>
6277

63-
<pre><code>```{r}
64-
```</code></pre>
6578

66-
* Code that you write inside chunks gets executed during the "knit" process and
67-
the results are shown below.
79+
* Knitting runs the code and prints its output
6880

69-
<pre><code>```{r}
70-
library(dplyr)
71-
```</code></pre>
81+
<pre><code>
82+
## Data
83+
84+
```{r}
85+
data <- read.csv("https://ndownloader.figshare.com/files/2292172")
86+
head(data)
87+
```
88+
</code></pre>
89+
90+
91+
### Chunk options
7292

7393
* Chunks have lots of useful options
7494
* Options are described at: [http://yihui.name/knitr/options/](http://yihui.name/knitr/options/)
7595
* Options will be listed in RStudio if you press tab inside
7696
the `{r}` brackets at the top of the chunk
7797

78-
<pre><code>```{r, message=FALSE}
79-
library(dplyr)
80-
```</code></pre>
81-
8298
<pre><code>```{r, message=FALSE}
8399
library(dplyr)
84100
library(ggplot2)
85101
```</code></pre>
86102

87-
<pre><code>```{r, message=FALSE, warning=FALSE}
88-
library(dplyr)
89-
library(ggplot2)
103+
* `cache=TRUE` reuses results of the code chunk in subsequent "knits". Save time
104+
re-calculating or re-downloading it each time.
105+
106+
<pre><code>```{r, cache=TRUE}
107+
data <- read.csv("https://ndownloader.figshare.com/files/2292172")
108+
head(data)
90109
```</code></pre>
91110

92111
* You can run code inside your text, too:
93-
* <code>`r cos(pi)`</code> turns into `-1` when you press the `Knit` button.
94-
* We will see and example of this later.
95-
96-
### Analysis Report Example
97-
98-
* Here's a text segment linked to a code chunk that begins an analysis report.
99112

100-
<pre><code>## Data
101-
102-
Data is from the [Portal project teaching database](http://figshare.com/articles/Portal_Project_Teaching_Database/1314459)
103-
published on Figshare. We need the surveys table for our analysis:
104-
105-
```{r, cache=TRUE}
106-
download.file("https://ndownloader.figshare.com/files/2292172",
107-
"surveys.csv")
108-
data <- read.csv("surveys.csv")
109-
```</code></pre>
113+
```
114+
The data includes `r length(unique(data$species_id))` species.
115+
```
110116

111-
* Setting `cache=TRUE` lets you reuse the results of this code chunk in
112-
subsequent "knits" instead of re-calculating or re-downloading it each time.
117+
### Analysis Example
113118

114-
<pre><code>## Analyze population time-series
119+
<pre><code>## Analysis
115120

116121
Get the time-series of counts for all species.
117122

@@ -120,6 +125,7 @@ time_series <-
120125
data %>%
121126
group_by(species_id, year) %>%
122127
summarize(count = n()) %>%
128+
filter(species_id %in% c('DM', 'DO', 'DS')) %>%
123129
na.omit()
124130

125131
head(time_series)
@@ -133,31 +139,19 @@ head(time_series)
133139
ggplot(time_series, aes(x = year, y = count)) +
134140
geom_point() +
135141
geom_line() +
136-
facet_wrap(~species_id) +
137-
scale_x_continuous(breaks = pretty_breaks(n=2))
138-
```
142+
geom_smooth() +
143+
facet_wrap(~species_id)
144+
```</pre></code>
139145

140-
## A simple model
146+
### Notebook
141147

142-
```{r, echo=FALSE}
143-
model <- data %>% group_by(year) %>%
144-
summarize(count = n()) %>%
145-
lm(count ~ year, data = .)
148+
* In RStudio run chunks using `Ctrl-Shift-Enter` or `Cmd-Shift-Enter`
149+
* Displays results in the editor
146150

147-
results <- anova(model)
148-
```</code></pre>
149-
150-
* Here's the example of an inline code chunk.
151+
* Notebook
151152

152-
<pre><code>We found a marginally significant linear relationship between the
153-
total count and year (p = `r round(results[["Pr(>F)"]][1], 3)`; see
154-
Table 1 for more details)
153+
* `output: html_notebook` or File -> New File -> R Notebook
155154

156-
```{r, echo=FALSE}
157-
knitr::kable(results, caption = "Table 1")
158-
```</code></pre>
159-
160-
* `knitr::kable()` is a handy way to make nice-looking tables from data frames.
161155

162156
### R Presentations
163157

@@ -173,6 +167,7 @@ author:
173167
date:
174168
autosize: true
175169

170+
176171
First Slide
177172
========================================================
178173

0 commit comments

Comments
 (0)