forked from rossihabibi/rprog2017-ps3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps3-rmd.Rmd
179 lines (136 loc) · 6.55 KB
/
ps3-rmd.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
---
title: "Tutorial 4 - Git and RMarkdown"
author: "Rossi Abi-Rafeh"
date: "11/12/2017"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, tidy = TRUE, tidy.opts=list(width.cutoff= 80))
```
## R Markdown Basics
### Create an R Markdown file
To create a new R Markdown file (.Rmd) in RStudio, select File -> New File -> R Markdown…, then choose the file type you want to create (Pdf, html, etc.)
For this tutorial, choose html.
The .Rmd file you just created comes with default text that you can change to include you own text or R analysis.
### R Markdown Syntax : The YAML Header
At the top of any R Markdown script is always the YAML header section enclosed by ---
This is the minimum code chunk that should be put there. By default this includes a title, author, date, and the file type you want as output. You can change from PDF (pdf_document) to HTML (html_document) here if you wish. Whatever you set in the header section applies to the whole document.
This is an example of a YAML header at the top of an .Rmd script which creates an html document.
---
title: "R Markdown Example"
author: Your Name
date: 13/11/2017
output: html_document
---
### Inserting Code
Code that is included in your .Rmd document should be enclosed by three backwards apostrophes ``` like the example below:
```{r}
summary(cars)
```
Inside an R code chunk, you can put whatever R code you want, including comments. Like this :
```{r}
summary(cars)
# cars is a dataset pre-loaded in R with information on speed and
# stopping distances of cars
```
The space inside the brackets is where you can assign rules for that code chunk. The code chunk above says that the code is R code.
### Instructions for Code Chunks
You can generate a plot like this:
```{r}
library(ggplot2)
ggplot(data = cars) + geom_point(mapping = aes(x = speed, y = dist))
```
You can create a dataframe:
```{r, tidy = TRUE}
library(tidyverse)
company <- c("Alphabet Inc", "Apple", "Facebook", "Amazon", "Ali Baba", "Snap Inc", "Criteo", "General Motors")
market_val <- c(714, 896, 518, 542, 477, 15, 2, 60)
# Market capitalization of company on the 13 Nov 2017 - from Yahoo! Finance
gafa_info <- data.frame(company, market_val)
head(gafa_info)
```
If you don’t want a code to appear in the report, but just want display the result, then you should set echo=FALSE in the code chunk instructions.
```{r, echo=FALSE}
company <- c("Alphabet Inc", "Apple", "Facebook", "Amazon", "Ali Baba", "Snap Inc", "Criteo", "General Motors")
listed <- c("NASDAQ", "NASDAQ", "NASDAQ", "NASDAQ", "NYSE", "NASDAQ", "NASDAQ", "NASDAQ")
gafa_info2 <- data.frame(company, listed)
head(gafa_info2)
```
If you want to load a package like knitr you must load the package in the .Rmd file. In case you do not want the entire code chunk and its output from appearing in the report, use include=FALSE. The code chunk will still be evaluated however.
```{r, include=FALSE}
library(knitr)
```
If you don't want a code chunk to be evaluated at all, then you use : eval = FALSE.
```{r, eval=FALSE}
library(NOPACKAGE) # there is no package called NOPACKAGE, but it does not give # an error message when compiling because the code chunk is not evaluated at all
```
### More code instructions
- eval: eval=TRUE– Is the code run and the results included in the output?
include: include=TRUE– Are the code and the results included in the output (the code is still run)?
- echo: echo=TRUE– Is the code displayed alongside the results?
- warning: warning=TRUE– Are warning messages displayed?
- error: error=FALSE– Are error messages displayed?
- message: message=TRUE– Are messages displayed?
- tidy: tidy=FALSE– Is the code reformatted to make it look “tidy”?
- results: results="markup"– How are results treated?
- cache: cache=FALSE– Are the results cached for future renders
- comment:comment="##"– What character are comments prefaced with?
- fig.width:, fig.width=7– What width (in inches) are the plots?
- fig.align: fig.align="left"– “left” “right” “center”
### Inserting Figures
To manually set the figure dimensions, you can insert an instruction:
```{r, fig.width=5, fig.height=5, echo=FALSE}
ggplot(data = cars) + geom_point(mapping = aes(x = speed, y = dist))
```
### Inserting Tables
R Markdown can print the contents of a data frame easily by enclosing the name of the data frame in a code chunk
```{r}
gafa_info
```
For nicer, better-looking tables, use the kable command from the knitr package.
```{r}
library(knitr)
kable(gafa_info, digits=1)
```
### Formatting Text
Markdown syntax can be used to change how text appears in your output file, here are a few common formatting commands
- header 1: # header 1
- header 2: ## header 2
- header 3: ### header 3
- header 4: #### header 4
- bold text: **bold** text
- italics text: *italics* text
- code text: `code` text
- LaTeX equation syntax: $A = \pi \times r^{2}$
You can also manually create small tables using markdown syntax.
`:----:`: Centre
`:-----`: Left
`-----:`: Right
`------`: Auto
For example:
| Teams | Wins | Loses |
|:------|:-----:|-------:|
| A | 21 | 4 |
| B | 19 | 6 |
| C | 17 | 8 |
### Compiling an R Markdown file
Select Knit -> Knit to HTML in RStudio.
A preview appears in the Viewer window in RStudio and the .html file is saved to your working directory. You can set your working directory location using:
setwd("~/...")
You can find your working directory using:
getwd()
## Create your first R Markdown File
1. Create a new R Markdown file (.Rmd) in RStudio.
2. Insert a YAML Header with title, author and date of your choice at the top of your .Rmd script.
3. Display the summary of “iris” dataset in your report. (Remember iris is a dataset pre-loaded in R that has the petal and sepal length and width for 3 types of iris.)
3bis. Add a header called "Summary of Iris dataset" to the previous step.
4. Load the package tidyverse without showing the code or the warnings in the report
5. Plot the scatterplot of petal length and sepal length, differentiated by Specie.
6. Set fig.width and fig.height of your plot to 5.
7. Create a small test dataframe with variables of your choice and dipslay it in your report.
8. Hide the code of the previous question from your report.
9. Create a new repository in your Github account. Call it rprog2017-ps3-stud
10. Clone the repository in your rprog folder.
11. Create an Rproject tied to the clone on your local machine.
12. Move the Rmd file you created today into that new folder.
12. Commit the changes you made (adding .Rproj and the .Rmd file), and push the changes. Check these two files are now on your github repo.