forked from jtleek/genstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_04_rmarkdown.Rmd
138 lines (91 loc) · 3.26 KB
/
01_04_rmarkdown.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
---
title: "R markdown lecture"
author: "Jeff Leek"
output:
rmarkdown::html_document:
toc: true
vignette: >
%\VignetteIndexEntry{R markdown}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
## Dependencies
This document depends on the following packages:
```{r}
library(devtools)
```
To install these packages you can use the code (or if you are compiling the document, remove the `eval=FALSE` from the chunk.)
```{r, eval=FALSE}
install.packages("devtools")
```
## Make the code pretty
Set the color scheme and pch.
```{r}
## Color scheme inspired by the RSkittleBrewer package
## https://github.com/alyssafrazee/RSkittleBrewer
tropical= c('darkorange', 'dodgerblue', 'hotpink', 'limegreen', 'yellow')
palette(tropical)
par(pch=19)
```
Unfortunately for R markdown documents the `pch` won't propagate unless you do something a little more messy. We'll hide this later, but for now this is what you need to do to set the default.
```{r global_options,warning=FALSE,message=FALSE}
## see ch. 10 Hooks of Xie's knitr book
library(knitr)
knit_hooks$set(setPch = function(before, options, envir) {
if(before) par(pch = 19)
})
opts_chunk$set(setPch = TRUE)
```
We will again hide this in future documents, but this shows how to set a default figure width and height, as well as setting plot margians.
```{r global-plot,warning=FALSE, message=FALSE}
knitr::opts_chunk$set(fig.width=5, fig.height=5, size="footnotesize",
warning=FALSE, message=FALSE)
knitr::knit_hooks$set(small.mar = function(before, options, envir) {
if (before) graphics::par(mar = c(5,5,1.5,1))
})
```
## Compiling documents
1. Try compiling this document using the "Knit HTML" button. What files are produced?
2. Edit the output to be "pdf_document" and recompile. What files are produced?
3. Edit the output to be "word_document" and recompile. What files are produced?
## Naming code chunks
The label "chunk1" tells you which part of the code was running in case you have errors. If you compile this document you'll see it under the "R markdown" window on R studio.
```{r chunk1}
x = rnorm(100)
plot(x,col=3)
```
## Headers
# This is a primary header.
## This a secondary header
### This is a tertiary header
## Lists
You can create bulleted and numbered lists in the following way.
* Bullet item 1
* Bullet item 2
* Bullet item 3
1. Numbered item 1
2. Numbered item 2
3. Numbered item 3
## Figures
The main arguments you might want to change are centering and figure height.
```{r chunk2, fig.height=5, fig.align="center" }
x = rnorm(100)
plot(x,col=3,pch=19)
```
## Other chunk arguments
Add a chunk option of `echo=FALSE` to hide the code.
```{r chunk4, echo=FALSE}
x = rnorm(100)
plot(x,col=3,pch=19)
```
Setting `cache=TRUE` makes it so that the code won't take as long to compile the second time. To see this, uncomment the code below and then run the document twice.
```{r longtime, cache=TRUE}
Sys.sleep(10)
```
## Session information
It is very useful to record the session information when performing analyses so you can collaborate with yourself in the future.
```{r session_info}
sessionInfo()
devtools::session_info()
```
It is also useful to compile the time the document was processed. This document was processed on: `r Sys.Date()`.