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
# 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
orR 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 |