Skip to content

Commit

Permalink
Merge pull request #728 from ethanwhite/knitr-update
Browse files Browse the repository at this point in the history
Rework knitr demo for smoother flow
  • Loading branch information
KristinaRiemer authored Nov 17, 2017
2 parents a4bf121 + 86f9813 commit 098553f
Showing 1 changed file with 69 additions and 74 deletions.
143 changes: 69 additions & 74 deletions materials/knitr.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,104 +14,109 @@ language: R

### Getting started

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

### Markdown

* Basic approach to formatting text
* Let's you do
* `# Headers`
* `*italics*`
* `**bold**`
* `[links](http://google.com)`
* Lists
* `*`
* `1.`
* `# Header`


<pre><code>
## Concept

Exploration of population dynamic patterns at **The Portal Project**.
How do counts of rodents like *Dipodomys* species change through time?

In this document I will:

1. Load data from the [Portal Project Teaching Database](http://figshare.com/articles/Portal_Project_Teaching_Database/1314459)
2. Process it into population time series
3. And make initial visualizations
</code></pre>

* Easy to read for humans
* Easy to convert into other things for computers
* Common on lots of websites

* Press `Knit` to create `HTML` from the document
* Can also create `PDF` & Word versions of our files
* `PDF` Requires `pandoc` and `TeX` installation
* Use the `Knit` dropdown or change `output: pdf_document`

* Markdown is common on lots of websites
* Used to create all of the exercises and lectures in this course
* Github will automatically render it
* [https://github.com/ethanwhite/CV/blob/master/CV.md](https://github.com/ethanwhite/CV/blob/master/CV.md)

<pre><code>Explore patterns in population dynamics at Portal.
### R chunks

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

### R chunks
<pre><code>
## Required Packages

* Set R code inside a set of <code>```</code> with the `{r}` designation
```{r}
library(dplyr)
library(ggplot2)
```
</code></pre>

<pre><code>```{r}
```</code></pre>

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

<pre><code>```{r}
library(dplyr)
```</code></pre>
<pre><code>
## Data

```{r}
data <- read.csv("https://ndownloader.figshare.com/files/2292172")
head(data)
```
</code></pre>


### Chunk options

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

<pre><code>```{r, message=FALSE}
library(dplyr)
```</code></pre>

<pre><code>```{r, message=FALSE}
library(dplyr)
library(ggplot2)
```</code></pre>

<pre><code>```{r, message=FALSE, warning=FALSE}
library(dplyr)
library(ggplot2)
* `cache=TRUE` reuses results of the code chunk in subsequent "knits". Save time
re-calculating or re-downloading it each time.

<pre><code>```{r, cache=TRUE}
data <- read.csv("https://ndownloader.figshare.com/files/2292172")
head(data)
```</code></pre>

* You can run code inside your text, too:
* <code>`r cos(pi)`</code> turns into `-1` when you press the `Knit` button.
* We will see and example of this later.

### Analysis Report Example

* Here's a text segment linked to a code chunk that begins an analysis report.

<pre><code>## Data

Data is from the [Portal project teaching database](http://figshare.com/articles/Portal_Project_Teaching_Database/1314459)
published on Figshare. We need the surveys table for our analysis:

```{r, cache=TRUE}
download.file("https://ndownloader.figshare.com/files/2292172",
"surveys.csv")
data <- read.csv("surveys.csv")
```</code></pre>
```
The data includes `r length(unique(data$species_id))` species.
```

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

<pre><code>## Analyze population time-series
<pre><code>## Analysis

Get the time-series of counts for all species.

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

head(time_series)
Expand All @@ -133,31 +139,19 @@ head(time_series)
ggplot(time_series, aes(x = year, y = count)) +
geom_point() +
geom_line() +
facet_wrap(~species_id) +
scale_x_continuous(breaks = pretty_breaks(n=2))
```
geom_smooth() +
facet_wrap(~species_id)
```</pre></code>

## A simple model
### Notebook

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

results <- anova(model)
```</code></pre>

* Here's the example of an inline code chunk.
* Notebook

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

```{r, echo=FALSE}
knitr::kable(results, caption = "Table 1")
```</code></pre>

* `knitr::kable()` is a handy way to make nice-looking tables from data frames.

### R Presentations

Expand All @@ -173,6 +167,7 @@ author:
date:
autosize: true


First Slide
========================================================

Expand Down

0 comments on commit 098553f

Please sign in to comment.