forked from perlatex/R_for_Data_Science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmarkdown.Rmd
283 lines (192 loc) · 6.17 KB
/
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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# (PART) tidyverse篇 {-}
# 可重复性报告 {#rmarkdown}
有时候,我们需要**展示和分享**我们的数据分析结果给同行、老板或者老师。 那么,为了让老板能快速地的理解我们的分析思路和方法,
最好的方法,就是将分析背景、分析过程、分析结果以及图表等形成**报告**,让读者能重复和验证我们的结果,确保结论的真实可信。
因此,本章就将介绍如何生成分析报告(可重复性报告)。
```{r, echo=FALSE}
library(rmarkdown)
```
## 什么是Rmarkdown
```{r out.width = '85%', echo = FALSE}
knitr::include_graphics("images/rmarkdown.png")
```
## markdown 基本语法
```{markdown, eval = FALSE, echo = TRUE}
# This is a title
# 第一章 (注意 "#" 与 "第一章"之间有空格)
## 第一节 (同上,"##" 与 "第一节"之间有空格)
This is a sentence.
Now a list begins:
- no importance
- again
- repeat
A numbered list:
1. first
2. second
__bold__, _italic_, ~~strike through~~
```
## Hello R Markdown
`Rstudio` create Rmd file : `File -> New File -> R Markdown`.
基本构成(图中绿色括号地方)
- metadata
- text
- code
```{r out.width = '85%', echo = FALSE}
knitr::include_graphics("images/rstudio-markdown.png")
```
点击knit(图中红色地方),选择想要输出的文档格式即可。
## 生成html文档
希望html文档有章节号、目录或者更好显示表格,可以修改头文件(用下面的内容替换Rmarkdown的头文件)
```yaml
---
title: Habits
author: John Doe
date: "`r Sys.Date()`"
output:
html_document:
df_print: paged
toc: yes
number_sections: yes
---
```
## 生成word文档
rmarkdown 生成的word功能不是很多,推荐使用[officedown](https://davidgohel.github.io/officedown/)
, [officer](https://davidgohel.github.io/officer/), [flextable](https://davidgohel.github.io/flextable/)宏包
## 生成pdf文档
pdf文档可以插入漂亮的矢量图和优雅的数学公式,所以备受同学们的喜欢。但往往我们写中文的时候,编译不成功。这里就来讲讲如何解决这些问题,推荐阅读[这里](https://bookdown.org/yihui/rmarkdown/),或者看这个[视频](https://www.bilibili.com/video/BV1Gf4y1R7md)。
*Instructions*
1. 安装最新版本 [R](https://cran.r-project.org/mirrors.html) (>3.5) 和 [RStudio Desktop](https://rstudio.com/products/rstudio/) (>1.2).
2. 安装pdf查看器,[sumatrapdf网站](https://www.sumatrapdfreader.org/download-free-pdf-viewer.html)
3. 安装 [LaTeX](https://www.latex-project.org/get/). 然而这个软件会比较大
(e.g. MacTeX is approximate 3.9G). 如果你之前没有安装过
LaTeX,我推荐你安装轻量级的 `tinytex`. 安装方法如下,打开R,然后再命令行输入:
```{r, eval = FALSE}
install.packages("tinytex")
tinytex::install_tinytex(dir = "D:\\Tinytex", force = T)
```
中途会有两次警告,按 “ 确定 ” 就可以了。
4. 修改头文件,用下面的内容替换Rmarkdown的头文件, 不要修改缩进
```yaml
---
title: 'Going deeper with dplyr'
author: "王小二"
date: "`r "\x60r Sys.Date()\x60"`"
output:
pdf_document:
latex_engine: xelatex
extra_dependencies:
ctex: UTF8
number_sections: yes
df_print: kable
toc: yes
classoptions: "hyperref, 12pt, a4paper"
---
```
## 使用方法
### 插入公式
我相信你已经熟悉了latex语法,那么我们在Rmarkdwon里输入
`$$\frac{\sum (\bar{x} - x_i)^2}{n-1}$$`,那么实际输出:
$$\frac{\sum (\bar{x} - x_i)^2}{n-1}$$
也可以使用latex的等式环境, 比如
```latex
$$\Theta = \begin{pmatrix}\alpha & \beta\\
\gamma & \delta
\end{pmatrix}$$
```
输出
$$
\Theta = \begin{pmatrix}\alpha & \beta\\
\gamma & \delta
\end{pmatrix}
$$
### 插入图片
````markdown
`r ''````{r, out.width='35%', fig.align='left', fig.cap='this is caption'}
knitr::include_graphics("images/R_logo.png")
```
````
```{r out.width = '35%', fig.align='left', fig.cap='this is caption', echo = F}
knitr::include_graphics("images/R_logo.png")
```
### 运行代码
```{r, echo = T}
summary(cars)
```
### 表格
<!-- ```{r tables-mtcars, echo = TRUE} -->
<!-- knitr::kable(iris[1:5, ], caption = "A caption") -->
<!-- ``` -->
````md
```{r tables-mtcars}`r ''`
knitr::kable(iris[1:5, ], caption = "A caption")
```
````
```{r tables-mtcars, echo = F}
knitr::kable(iris[1:5, ], caption = "A caption")
```
需要更优美的表格,可参考[这里](https://haozhu233.github.io/kableExtra/)
### 生成图片
````md
```{r}`r ''`
plot(pressure)
```
````
```{r out.width = '85%', echo=FALSE}
plot(pressure)
```
或者
````md
```{r, out.width = '85%', fig.showtext = TRUE}`r ''`
library(tidyverse)
library(nycflights13)
library(showtext)
showtext_auto()
flights %>%
group_by(dest) %>%
summarize(
count = n(),
dist = mean(distance, na.rm = TRUE),
delay = mean(arr_delay, na.rm = TRUE)
) %>%
dplyr::filter(delay > 0, count > 20, dest != "HNL") %>%
ggplot(mapping = aes(x = dist, y = delay)) +
geom_point(aes(size = count), alpha = 1 / 3) +
geom_smooth(se = FALSE) +
ggtitle("这是我的标题")
```
````
```{r, fig.showtext = TRUE, echo=FALSE}
library(tidyverse)
library(nycflights13)
library(showtext)
showtext_auto()
flights %>%
group_by(dest) %>%
summarize(
count = n(),
dist = mean(distance, na.rm = TRUE),
delay = mean(arr_delay, na.rm = TRUE)
) %>%
dplyr::filter(delay > 0, count > 20, dest != "HNL") %>%
ggplot(mapping = aes(x = dist, y = delay)) +
geom_point(aes(size = count), alpha = 1 / 3) +
geom_smooth(se = FALSE) +
ggtitle("这是我的标题")
```
```{r echo=FALSE}
# pdf文档模板
xfun::embed_file('./data/my_pdf_document.Rmd')
```
## 延伸阅读
* Markdown tutorial https://www.markdowntutorial.com (10分钟学完)
* LaTeX tutorial https://www.latex-tutorial.com/quick-start/
* Rmarkdown 介绍 https://bookdown.org/yihui/rmarkdown/
* Rmarkdown 手册 https://bookdown.org/yihui/rmarkdown-cookbook/
```{r, echo = F}
# remove the objects
# rm(list=ls())
#rm(ar, df, die, dt, fac, list1, m, n, vec, x, y)
```
```{r, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```