This document is a work in progress, please give us feedback

1 Reproducible documents

1.1 Before we start

  1. Install the rmarkdown package install.packages("raster")
  2. Open a new R markdown file *.Rmd, File -> New file -> R markdown
  • Enter a title and author(s).
  • Choose ‘Default Output Format’ as HTML (for now)
  1. Save the Rmd file
  2. knit the document (Windows and Linux, Ctrl+Shift+K; Mac, Command+Shift+K)

1.2 How does it work?

The rmarkdown R package uses and interacts with other packages and programs to create a final document. For example, knitr converts the Rmd file into a markdown (.md) file, which is a common format for several websites (including the lectures and guides in this course). knitr runs chunks and inline R code and includes the output to a markdown document. rmarkdown then calls the pandoc program to convert the markdown into different formats. Note that to create a PDF, you need to install Latex. The bookdown R package is necessary to number figures, tables and also it can be used to generate books, such as R for Data Science

Source: A guide to reproducible code in Ecology and Evolution by the BES

1.3 Main parts of an R Markdown file

1.3.1 The YAML Header

Included at the top of the R Markdown script, the YAML header enclosed by ---- defines the title, author, date of the document and most importantly the file type you want to output (html, pdf, md, word).

---
title: "Example"
author: "YOUR NAME HERE"
date: "01/30/2017"
output: html_document
---

You can also alter the appearance and style of your document with different arguments. For example, toc: true will include a table of content. Click Here to see other arguments.

---
title: "Example"
author: "YOUR NAME HERE"
date: "01/30/2017"
output: 
  html_document:
  toc: true
---

1.3.2 Markdown Basic syntax

R Markdown Cheat sheet

# Header 1
## Header 2
### Header 3

*Italics*
**Bold**
`inline code`
[link](https://rmarkdown.rstudio.com/)

* Bullet Lists
  + Subitems

  • Markdown Quick Reference Help-> Markdown Quick Reference
  • R Markdown reference guide Help->Cheatsheets-> R Markdown Cheat Sheet or R Markdown Reference Guide

1.3.3 Code Chunks

To run R code inside an R Markdown

  • The keyboard shortcut Cmd/Ctrl + Alt + I
  • Or “Insert” button icon in the toolbar
```{r}
msg<-"Hello world"
msg
```
## [1] "Hello world"

1.3.4 Chunks options

To modify how they run and how they appear in the document

Show only the output

```{r echo=FALSE}
msg<-"Hello world"
msg
```
## [1] "Hello world"

Run code but does not display code or ouput

```{r include=FALSE}
msg<-"Hello world"
msg
```

Print code but does not run it

```{r eval=FALSE}
msg<-"Hello world"
msg
```

Do not display messages or warnings

```{r message=FALSE,warning=FALSE}
library(tidyverse)
library(knitr)
```

Naming chunks

```{r hello_world, eval=FALSE}
msg<-"Hello world"
msg
```

1.3.5 Inline Code

To incorporate R outputs into the text

The class of the object msg is `r class(msg)` and it has `r length(msg)` elements

The class of the object msg is character and it has 1 elements

1.3.6 Inserting plots

By default

```{r plot1}

data(iris)

ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_boxplot()

```

Setting figure dimensions

```{r plot2, fig.width=2.5, fig.height=7.5}

data(iris)

ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_boxplot()

```

You can include the argument dev="svg" to render your plot as a vector format .svg instead of the default format .png

1.3.7 Tables

By default

```{r }

head(iris)

```
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

A bit messy right? For a more pleasing table, we can use the function kable from the R package knitr

```{r table1}

head(iris)

```
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Or you can direclty create table in markdown syntax

| Species| Var1  | Var2   |
|:------ |:-----:|-------:|
| A      | 20    | 0.65   |
| B      | 20    | 0.95   |
| C      | 20    | 0.15   |
Species Var1 Var2
A 20 0.65
B 20 0.95
C 20 0.15

2 Reproducible slide presentations

  1. Open a new R markdown file *.Rmd, File -> New file -> R markdown -> Presentation
  • Enter a title and author(s).
  • Choose ‘Default Output Format’ as HTML (ioslides) (for now)
  1. Save the Rmd file

  2. knit the document (Windows and Linux, Ctrl+Shift+K; Mac, Command+Shift+K)

  3. Open the html in a browser

2.1 Display Modes

When you open the html in a browser, use the following character keyboard shortcuts to enable alternate display modes:

  • f enable fullscreen mode

  • w toggle widescreen mode

  • o enable overview mode

  • h enable code highlight mode

Press Exit to exit all the display modes

2.2 The YAML Header

---
title: "Example"
author: "YOUR NAME HERE"
date: "01/30/2017"
output: ioslides_presentation
---

By default the argument echo=FALSE. Check the setup at the beginning of the document.

```{r}

knitr::opts_chunk$set(echo = FALSE)

```

2.3 Markdown Basic syntax


## Markdown Basic syntax


*Italics*

**Bold**

`inline code`

[link](https://rmarkdown.rstudio.com/)

- Bullet 1
- Bullet 2
- Bullet 3

2.4 Plots

## Slide with R Output
```{r echo=TRUE, fig.height=4}

data(iris)

ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_boxplot()

```

2.5 Tables

## Tables
```{r table}

kable(head(iris))

```

**You can see additional ioslides arguments and options in the following link https://rmarkdown.rstudio.com/ioslides_presentation_format.html**