-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr-programming.Rmd
627 lines (435 loc) · 22.1 KB
/
r-programming.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# R 语言 {#rprogramming}
```{r echo=FALSE}
library(magrittr)
```
## 安装和配置
### 安装
- ubuntu 安裝 R
参考https://cran.r-project.org/bin/linux/ubuntu/README.html
```sh
# 添加源 以 16.04 xenial 为例,根据自己的ubuntu版本添加相应源
deb https://cloud.r-project.org/bin/linux/ubuntu xenial-cran35/
# 密匙
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo apt-get update
sudo apt-get install r-base r-base-dev
```
- CentOS 安装 R
```sh
yum install epel-release
yum install R-core R-core-devel
```
- 升级 R 版本后装, minor 以上的升级需要重新安装包(`x.y.z`中的`y`).
参考[这里](https://rstats.wtf/maintaining-r.html#how-to-transfer-your-library-when-updating-r).
### 配置
- 设置cran和bioconductor国内源
可以在用户主目录下新建`.Rprofile`文件,添加:
```r
# cran mirror
options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
# bioconductor mirror
options(BioC_mirror = "http://mirrors.ustc.edu.cn/bioc/")
```
### 安装包
- centos 7 安装 hdf5r
见[这](https://www.choyang.me/zh/post/centos-7-r-install-hdf5r/)
- cenos 7 安装 stringr
stringr 依赖 [stringi](https://cran.r-project.org/web/packages/stringi/),
stringi 要求 ICU4C >= 55, 而centos 7 中 icu版本为50.2。我们可以从 stringi 的
[github 仓库](https://github.com/gagolews/stringi)下载源代码,里面包含ICU安装
包,直接安装即可。
```r
wget https://github.com/gagolews/stringi/archive/v1.4.6.tar.gz - O stringi-1.4.6.tar.gz
install.packages("stringi-1.4.6.tar.gz")
install.packages("stringr")
```
- `install_github()`安装包的时候出现错误`Error in file.copy(file.path(R.home("doc"), "html", "R.css"), outman)`,
系统是centos
因为安装 R 的时候缺少`file.path(R.home("doc"), "html", "R.css")`,需要手动创建,
参考[这](https://github.com/r-lib/devtools/issues/2084)
```R
if (!file.exists(x)) {
dir.create(x, recursive=TRUE)
file.copy(system.file("html/R.css", package="stats"), x)
}
```
- ubuntu 20.04 安装 Cairo 出现错误, `lib-backend.c:34:10: fatal error: X11/Intrinsic.h: No such file or directory`
已经安装了包的系统依赖 libcairo2-dev, 安装 `libxt-dev`即可,参见 [这里](https://stackoverflow.com/questions/23642353/error-message-installing-cairo-package-in-r?answertab=active#tab-top)
## R 包开发
##### 1. github中添加包的 code coverage {-}
1. 使用`usethis::use_coverage()`, 添加test coverage,
2. 运行后会创建文件`codecov.yml`控制台会显示类似下面markerdown文本,粘贴至
README文件添加badage
```
[](https://codecov.io/gh/yiluheihei/rlatexmath?branch=master)
```
3. 用github账号登录[codecov.io](codecov.io), 添加该仓库至codecov赋予其权限,
然后会弹出一个token (如下图所示)
```{r codecov-token,echo=FALSE}
knitr::include_graphics("image/codecov-token.png")
```
4. 利用`covr::codecov`检测包的test coverage并上传至codecov
```r
covr::codecov(token = "YOUR_TOKEN_GOES_HERE")
```
##### 2. 包中 README.md 中用到的图片存放位置 {-}
因为 cran 中生成 readme 只允许放在少数文件夹下,如`man/figures/`
[**pkgdown**](https://github.com/ropensci/rotemplate/issues/19#issuecomment-506315565)
跟 cran 保持一致。所以 README.md 中图片建议存放在`man/figures/`,
如果是由 Rmd 文件生成,设置 knitr 图片保存路径
`knitr::opts_chunk(fig.path = "man/figures/")`
##### 3. 写 unit test 的时候如果需要读取用`save()`或者`saveRDS()`保存外部数据,出现warning {-}
Added dependency on R >= 3.5.0 because serialized objects in serialize/load
version 3 cannot be read in older versions of R
这是因为 R 在 3.5.0 版本后修改了这两个函数的参数`version`默认为`3`, 保存数据的时候设置参数`version = 2`即可。
##### 4. 按照[r-pkgs](https://r-pkgs.org/data.html#documenting-data)建议写导出`data`的文档,check 的时候出现warning (`Lazyload: TRUE`) {-}
> "Variables with usage in documentation object '<data-name>' but not in
code: '<data-name>'
写成如下形式解决问题,手动指定文档的`name`,最后一行用`NA`替代`"<data-name>"`:
```R
#' title
#'
#' Description
#'
#' @name <data-name>
NA
```
##### 5. 帮助文档中公式 {-}
采用<code>\\eqn{*latex*}{*ascii*}</code>(行内公式), 和
<code>\\deqn{*latex*}{*ascii*}</code>(以段落显示的公式).
其中 *`latex`* 表示 **LATEX** 公式, *`ascii`* 表示帮助中想要展示的公式,
如$\frac{\lambda^x e^{-\lambda}}{x!}$, 应写成:
```
\eqn{\frac{\lambda^x e^{-\lambda}}{x!}}{%
\lambda^x exp(-\lambda)/x!}
```
此外,也可以采用 <code>\\eqn{*latexascii*}</code>, 表示 <code>*latex*</code> 和
<code>*ascii*</code>表示方法相同的时候写一个参数即可。参考
[这里](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Mathematics).
##### 6. 依赖其他包中的数据 {-}
参考[这里](https://stackoverflow.com/questions/48715141/how-do-i-load-data-from-another-package-from-within-my-package).
1. 直接使用`::`, `<pkg>::<data>`;
2. 如果多次用到该数据,减少多次使用`::`的开销,可以直接复制数据到自己的包中
`<mydata> <- <pkg>::<data>`;
3. 如果可以修改源数据的包,可以导出数据, 在 `<pkg>` 的 `NAMESPACE` 中添加
`@export(<data>)`, 或`roxygen2`添加`@export`字段。然后`@importFrom <pkg> <data>`
就可直接使用数据。(然而老哈的 [r-pkgs](https://r-pkgs.org/data.html#documenting-data)
中说:
> Never \@export a data set.
##### 7. Mac升级 Big Sur后R 包开发环境被破坏,改进方法 {-}
参考[这里](https://stackoverflow.com/questions/65251887/clang-7-error-linker-command-failed-with-exit-code-1-for-macos-big-sur)
1. 升级 xcode
2. 从这里安装gfortran:https://github.com/fxcoudert/gfortran-for-macOS
3. 修改`~/.R/Makevars`
```sh
# 备份~/.R/Makevars, 如果没有直接新建即可
mv ~/.R/Makevars ~/.R/Makevars.bak
vi ~/.R/Makevars # 添加
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/10.2.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CXX1X=/usr/local/gfortran/bin/g++
CXX98=/usr/local/gfortran/bin/g++
CXX11=/usr/local/gfortran/bin/g++
CXX14=/usr/local/gfortran/bin/g++
CXX17=/usr/local/gfortran/bin/g++
LLVM_LOC = /usr/local/opt/llvm
CC=/usr/local/gfortran/bin/gcc -fopenmp
CXX=/usr/local/gfortran/bin/g++ -fopenmp
CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS=-L/usr/local/opt/gettext/lib -L$(LLVM_LOC)/lib -Wl,-rpath,$(LLVM_LOC)/lib
CPPFLAGS=-I/usr/local/opt/gettext/include -I$(LLVM_LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
```
## Rstudio
- rstudio git 选择所有文件add并commit
在commit界面,`ctrl/command + A`全选,然后回车即可,参考[这里
](https://stackoverflow.com/a/33913671/3993246)
- ubuntu rstudio不支持中文
这是因为 rstudio 自带的QT5不支持 fctix (搜狗拼音就是依赖fctix),设置 rstuido 使用系统自带的QT即可,步骤如下
```
# 1.系统自带 qt 的 fctix 插件链接到 rstudio 相应文件夹下
sudo ln -s /usr/lib/$(dpkg-architecture -qDEB_BUILD_MULTIARCH)/qt5/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.so /usr/lib/rstudio/plugins/platforminputcontexts/
```
重启 Rstudio 即可
### snippet
在编写脚本的时候,在脚本开始写上该脚本的信息(如作者,时间,脚本说明)
```r
snippet header_script
################################################################################
## Description:
## Author:
## Create Time: `r paste(date())`
## Updated Time:
################################################################################
```
脚本的分段注释, 描述该段代码的作用
```r
snippet header_section
################################################################################
################################################################################
```
### Rstudio 常用快捷键
#### Navigating code (代码导航)
##### 跳转到project中的文件/函数(定义)
- `ctrl + .`, 在弹出框中输入文件名或者函数名, 也可以在工具栏的`Go to File/Function` 搜索框中输入要搜索的文件名或者函数名
- `F2`
- 按住`ctrl`, 点击左键
##### 源文件导航
- 跳转到指定行, `Alt+Shift+G` (Mac: `Option+Shift+Cmd+G`).
- 点击源代码编辑器(source editor)下方的状态栏从函数列表中选择函数会跳转到该函数
的源码.
##### 前进和后退
`Ctrl+F9/Ctrl+F10` (Mac: `Cmd+F9/Cmd+F10`)或者是源代码编辑器上方工具栏最左边的方向
箭头。适用于以下情况:
- 不同source文件之间的切换
- 跳转到函数定义处
- 跳转到指定行
#### Code folding (代码折叠)
以下几类代码可以折叠:大括号`{}`内的代码块(如函数或者条件语句); Rmarkdown 中代码
块 (code chunks) 和标题,及代码段 (code section);任何以四个以上的`-`或`=`或`#`字
符为结尾的注释行(注释行中开始`#`的个数可以为多个)。
```
# Section One ---------------------------------
# Section Two =================================
### Section Three #############################
```
- 折叠, `Alt+L`
- 展开, `Shit+Alt+L`
- 全部折叠, `Alt+O`
- 全部展开, `Shit+Alt+O`
- 插入代码块, `Ctrl+Shit+R` (Mac: `Cmd+Shift+R`)
- 代码块跳转, `Shit+Alt+J`
## R 基础
- 根据序列长度进行迭代(循环)的时候,`1:seq_along(y)`和`1:length(y)`有什么区别?
应使用`1:seq_along(y)`。当`length(y) = 0 `时`seq_along(y)`返回`integer(0)`,基于`1:seq_along(y)`的循环会抛错; 而`1:length(y)`表示`1:0`的迭代,可能出现意想不到的结果。参见R for Data Science [@grolemund2019]
>You might not have seen `seq_along()` before. It’s a safe version of the familiar `1:length(l)`, with an important difference: if you have a zero-length vector, `seq_along()` does the right thing
```{r seq-along, collapse=TRUE, error=TRUE}
y <- vector("double", 0)
# 抛错
seq_along(y)
sapply(1:seq_along(y), function(x) x + 1)
# 正常计算
1:length(y)
sapply(1:length(y), function(x) x + 1)
```
- 如果我们在`~/.Rprofile`添加了初始化设置,那么如果当前工作目录中有含有`.Rprofile`的时候(如在github上fork的别人的项目中含有该文件),就会忽略`~/.Rprofile`中的设置。
解决方法`# ~/.Renviron `中设置site profile变量`R_PROFILE=~/Rprofile.site`,然后把之前`.Rprofile`的设置拷贝到`~/Rprofile.site`即可。这是因为R的启动的时候会在读取`.Rprofile`之前读取`~/Rprofile.site`中的设置。
- 设置Alfred快捷打开rstudio的project
*Preferences > Features > Default Results > Advanced*,然后把任何`.Rproj`文件拖入窗口,[参考于此](https://whattheyforgot.org/project-oriented-workflow.html#tricks-for-opening-projects)
- 安装rlang的时候出现错误`复制 C:\R-3.6.0\library\00LOCK-rlang\00new\rlang\libs\x64\rlang.dll到 C:\R-3.6.0\library\rlang\libs\x64\rlang.dll 时出了问题:Permission denied`
因为如果R包如果包含c++/c/Fortran代码(如rlang),如果已经加载了这种包,在对其进行升级或者重装
的时候就会出现类似上述错误。关掉加载这些包的R,才能继续重装或升级。
- 出现错误:`C stack usage xxxx is too close to the limit`
可能是因为调用函数存在无限循环的递归调用
```r
foo <- function(...) {
foo(...)
}
```
- `data.frame`作为list的一个元素
在写函数的时候有时候需要利用参数列表(列表的名称表示参数名)加`do.call()`来调用
执行函数。一般形如`c(list(arg1=value1, arg2=value2), arg3=value3)`,
如果有参数的类别是`data.frame`的话(value3),因为`data.frame`实际上也是列表,
这样`data.frame`的每一列都会成为列表的一个元素。为了保证`data.frame`作为列表的
一个元素,对该参数在构建参数列表的时候要显式的使用`list(df)`,
`c(list(arg1=value1, arg2=value2), arg3=list(value3))`
- `switch`中匹配`NA`, 使用用反引号
```r
swithc(
var,
var1 = ...,
var2 = ...,
`NA` = ...
)
```
## tidyverse 数据分析
- `data.frame` 转置(transpose)
tidyverse 中 默认操作数据结构为 `tibble`, 用于替代 R 中`data.frame`,`tibble`
默认没有行名。下面对用`t()`和 tidyr 方法转置作比较:
用`t()`函数
```{r t-transpose}
# 有行名的数据
df <- data.frame(a = 1:3, b = 4:6)
row.names(df) <- paste0("r", 1:3)
# as.data.frame(), 保持行名和列名
t(df) %>% as.data.frame()
# as_tibble, 默认删除行名
t(df) %>% tibble::as_tibble()
# 可以用`rowname = NA`保持行名, 左上角有个`*`表示这个tibble有行名
t(df) %>% tibble::as_tibble(rownames = NA)
```
用 tidyr 中函数
```{r tidyr-transpose}
# 保持行名和列名, column_to_rownames用于设置行名,返回data.frame
tibble::rownames_to_column(df) %>%
tidyr::pivot_longer(-rowname) %>%
tidyr::pivot_wider(names_from = "rowname", values_from = "value") %>%
tibble::column_to_rownames("name")
```
此外还可以使用 `purrr::transpose`
```{r purrr-transpose}
# 行名列名都丢失,transpose 去除了原有行名
purrr::transpose(df) %>%
purrr::map(unlist) %>%
dplyr::bind_cols()
```
## 可视化 {#visualization}
### 基础图形 {#base-plot}
- `par("pin")`, `par("usr")`
`par("pin")`表示当前图形的尺寸用英寸表示(width * height);`par("usr")`表示当前图形的坐标范围`c(xmin, xmax, ymin, ymax)`
- 颜色代码的可视化,以便观察颜色
- `image(1, 1, as.matrix(1), col = "#E64B35")`
- `scales::show_col("#E64B35")`
### ggplot
- 什么是**individal geom** 和 **collective geom**?
基于一行数据进行作图的`geom`称为**individal geom**,如`geom_point`每行数据映射到图形中的一个点;基于多行数据作图的`geom`称为**collective geom**, 如`geom_bar`对多行数据进行统计变换计算其频数,也就是说多行数据映射到图形中的一个**bar**.
- 如何正确使用 **ggplot** `aes(group)`
见[这篇博客](https://www.choyang.me/zh/post/ggplot-aes-group-var/)
- 修改连续坐标轴的范围
见[这篇博客](https://www.choyang.me/zh/post/ggplot-range-of-continuous-axis/)
- 坐标轴label移动到图形内部
见[此](https://www.choyang.me/zh/post/moving-axis-label-left-justified-inside-plot-area/)
- 修改离散坐标的顺序
见[这篇博文](https://www.choyang.me/zh/post/ggplot-reorder-discrete-axis/)
- 添加文本标注
使用`Inf`或`-Inf`表征在边界添加文本
```{r text-annotation-position}
# 例子源自https://r4ds.had.co.nz/graphics-for-communication.html
library(ggplot2)
label <- tibble::tibble(
displ = Inf,
hwy = Inf,
label = "Increasing engine size is \nrelated to decreasing fuel economy."
)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_text(aes(label = label), data = label, vjust = "top", hjust = "right")
```
- 图形添加边框
设置`theme(plot.background = element_rect())`
```{r round-rect}
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
theme(plot.background = element_rect(color = "black"))
```
- 等高线图`geom_contour()`自定义levels
手动指定参数`breaks`(帮助文档中找不到该参数说明),参考[此](https://stackoverflow.com/questions/19658088/custom-levels-in-ggplot2-contour-plot).
- ggplot中文字体
[见此](https://www.choyang.me/zh/post/ggplot-chinese-font/)
- ggplot分面添加tag (如A,B, C),可参考[此问题](https://community.rstudio.com/t/how-to-automatically-add-text-annotations-or-tags-outside-of-faceted-plots/13700)
```{r facet-tag}
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(vars(class))
egg::tag_facet_outside(p)
```
## 字符 {#char}
### 正则表达式 {#rexpr}
- 正则表达式字符列表`[]`用法:`[]`中的字符匹配字符本身,如`[.]`匹配.,而不是任意字符,因此可以使用`[.]`代替`\\.`,一些在`[]`内有特殊意义的字符除外,如`-`表示范围,`^`不包含这些字符,`]`结束括号,`\`在字符串中表示转义。
- 正则表达式默认是进行贪婪匹配,即匹配最长字符串,在正则表达式后边加?表示最短匹配
```{r collapse=TRUE}
# 匹配到两个a,然后删除
sub("a+", "", "aac")
# 匹配到一个a,然后删除
sub("a+?", "", "aac")
```
- 表示重复次数的`*`(任意次)、`?`(0或1次)、`+`(一次以上)优先级较高
```{r collapse=TRUE}
# b出现一次以上
sub("ab+", "", "ababc")
# ab出现一次以上
sub("(ab)+", "", "ababc")
```
## 可重复性研究 {#reproducible-research}
### Rmarkdown - Knitr
- Rmarkdown 输出 pdf 支持中文
使用[rticles](https://github.com/rstudio/rticles)包, 在 `yaml` 头文件中指定
输出格式为`rticles::ctex`. 参考[这里](https://github.com/rstudio/rticles/blob/master/inst/rmarkdown/templates/ctex/skeleton/skeleton.Rmd)
```
documentclass: ctexart
output:
rticles::ctex
```
- knitr多个代码块的输出折叠到一个pre块中
`collapse = TRUE`
- 输出为github的markdown格式
`github_document`产生github的markdown格式,比如`README.Rmd`生成`README.MD`
- knitr 数据较大时设置缓存
`cache =TRUE`,对于较大数据可能会抛出错误;这时候设置`cache.lazy = FALSE`即可,答案在[这](https://stackoverflow.com/questions/39417003/long-vectors-not-supported-yet-error-in-rmd-but-not-in-r-script)和[这](https://github.com/yihui/knitr/issues/572)
- 代码块中的输出多个图形,怎么输出为一副图形?
`figure.show`默认为`asis`图形位于产生图形代码后面,多个作图语句就会产生多幅图形,`figure.show='hold'`就代码块结尾显示所有图形,即一个代码块产生一个图形。
- Rmarkdown 中代码块内斜体(或其他 markdown 标记语法)
把 一般的 markdown 标记放在代码外围,如``*`斜体代码`*``, 显示为*`斜体代码`*。
如果是正常字体和斜体混合借助`<code>`元素: `<code>正常*斜体*</code>`,显示为
<code>正常*斜体*</code>.
### bookdown
- 章节去除编号 章节后面添加 {-}
- 汉话:章节标题汉化,https://bookdown.org/yihui/bookdown/internationalization.html。配置文件`_bookdown.yml`
```
language:
ui:
chapter_name: ["第", "章"]
```
>The chapter_name field can be either a character string to be prepended to chapter numbers in chapter titles (e.g., 'CHAPTER '), or an R function that takes the chapter number as the input and returns a string as the new chapter number (e.g., `!expr function(i) paste('Chapter', i))`. If it is a character vector of length 2, the chapter title prefix will be `paste0(chapter_name[1], i, chapter_name[2])`, where `i` is the chapter number.
- 出现错误`Error in split_chapters(output, gitbook_page, number_sections, split_by, :Automatically generated filenames contain duplicated ones: -`
表示由`split_by`自动生成章节标题出现重复标题而出现错误。中文标题会出现这种错误,章节标题手动添加`{$ID}`即可,参见[这](https://d.cosx.org/d/420172-bookdown-split-chapters)和[这](https://github.com/rstudio/bookdown/issues/520)
如果问题还存在, 设置 `split_by`为`none`或`rmd`,参考[这里](https://github.com/rstudio/bookdown/issues/902).
##### github action 部署 bookdown 到 github page {-}
参考 https://github.com/r-lib/actions/blob/v2-branch/examples/bookdown.yaml.
使用[r-lib/actions/setup-r-dependencies@v2](https://github.com/r-lib/actions/tree/v2-branch/setup-r-dependencies)根据 DESCRIPTION 安装依赖的包;如果要生成 PDF
文件安装 [TinyTex](https://github.com/r-lib/actions/tree/v2-branch/setup-tinytex).
```yaml
# Workflow derived from https://github.com/r-lib/actions/tree/master/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
name: bookdown
jobs:
bookdown:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v2
- uses: r-lib/actions/setup-pandoc@v1
- uses: r-lib/actions/setup-r@v1
with:
use-public-rspm: true
- uses: r-lib/actions/setup-renv@v1
# 根据 DESCRIPTION 安装依赖的包
# https://github.com/r-lib/actions/tree/v2-branch/setup-r-dependencies
- uses: r-lib/actions/setup-r-dependencies@v2
- name: Cache bookdown results
uses: actions/cache@v2
with:
path: _bookdown_files
key: bookdown-${{ hashFiles('**/*Rmd') }}
restore-keys: bookdown-
# 如果生成 pdf 安装TinyTex
- name: Install TinyTeX
uses: r-lib/actions/setup-tinytex@v1
env:
# install full prebuilt version
TINYTEX_INSTALLER: TinyTeX
- name: Build site
run: |
Rscript -e 'bookdown::render_book("index.Rmd", quiet = TRUE)'
Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::pdf_book')"
- name: Deploy to GitHub pages 🚀
uses: JamesIves/[email protected]
with:
branch: gh-pages
folder: _book
```
### tinytex
- pkgdown编译jss文档的时候出现,`! LaTeX Error: File `ae.sty'not found.` 说明缺少ae包,
```{r eval=FALSE}
tlmgr_search('ae.sty')
# 提示remote repository 版本太旧,2018 < 2019,这时候需要升级texlive
tinytex::reinstall_tinytex()
tlmgr_search('ae.sty')
# ae:
# texmf-dist/tex/latex/ae/ae.sty
#安装即可
tlmgr_install('ae')
```