3 Rmarkdown语法
3.1 基本语法
- 上标
text^supscript^a2 - 下标
text~subscript~k1 - 脚注
text^[footnote]某观点1
代码块设置,使块中文字右对齐,仅适用于 pdf
3.2 编译PDF
install.packages("tinytex")
tinytex::install_tinytex(dir = "D:\\Tinytex", force = T)Rmd 文件的 metadata:
---
output:
pdf_document:
latex_engine: xelatex
extra_dependencies:
ctex: UTF8
number_sections: yes
df_print: kable
toc: yes
classoptions: "hyperref, 12pt, a4paper"
---3.4 R表格
表格尽量使用代码块,便于统一编号
table <- tibble::tibble(ID = 1:5, name = letters[1:5])
knitr::kable(table, caption = "order of letters") # 用三线表格显示数据| ID | name |
|---|---|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
3.4.1 kableExtra package
更强大、优美的表格。主要包括:
- 表格整体风格,包括斑马纹、悬浮效果、行高、响应式表格宽度、浮动位置、字体大小
- 行、列的样式,参数中可以用 CSS 语法
- 格子的样式
- 行、列的分组,以及分组下部分行的缩进
- 表格的脚注
- 表格的保存
文档参见:
https://haozhu233.github.io/kableExtra/
http://haozhu233.github.io/kableExtra/awesome_table_in_html_cn.html
# 例
library(kableExtra)
mtcars[1:5, 1:4] %>%
kable("html") %>%
kable_styling(
bootstrap_options = c(
"striped", # 明暗条纹
# "hover", # 鼠标划过高亮
# "condensed", # 紧凑行高
"responsive" # 响应屏幕宽度
),
full_width = F
) %>%
add_header_above(c(" " = 1, "Group1" = 2, "Group2" = 2))| mpg | cyl | disp | hp | |
|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 |
| Datsun 710 | 22.8 | 4 | 108 | 93 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 |
3.4.2 三线表
bruceR::print_table() 输出三线表至 console 或 .doc
- 主要参数
x:矩阵(matrix)、数据框(data.frame、data.table等)或能够提取出模型系数表的统计模型(lm、glm、lmer、glmer等)digits:保留的小数位数,默认是 3 位小数file:保存的 Word 文档(.doc),默认输出到 R 控制台- ……(见原始帮助文档)
model <- lm(Temp ~ Month + Day + Wind + Solar.R, data = airquality)
print_table(model)#> ──────────────────────────────────────────────
#> Estimate S.E. t p
#> ──────────────────────────────────────────────
#> (Intercept) 68.770 (4.391) 15.662 <.001 ***
#> Month 2.225 (0.441) 5.047 <.001 ***
#> Day -0.084 (0.070) -1.194 .234
#> Wind -1.003 (0.176) -5.695 <.001 ***
#> Solar.R 0.027 (0.007) 3.991 <.001 ***
#> ──────────────────────────────────────────────
print_table(model, file = "./export/Results.doc")#> √ Table saved to "C:/Users/humoo/OneDrive/ICT/Website/static/notes/Programming-Language/R/06-Rmarkdown/./export/Results.doc"
3.7 下载资源文件/文件夹
3.7.1 xfun package
在页面中显示一个下载链接。
该包理论上有三个函数,xfun::embed_file(), xfun::embed_files()和xfun::embed_dir(),但只有第一个能用,后两个似乎有bug.
xfun::embed_file(path, name = basename(path), text = paste("Download", name), ...),后两个参数均可省略。
# a single file
xfun::embed_file(
path = "./figure/resource.png",
name = "scatter",
text = "Download resource files"
)3.7.2 downloadthis package
3.7.2.1 Data frames, lists, or any R object
mtcars %>%
downloadthis::download_this(
output_name = "mtcars data set",
output_extension = ".csv",
button_label = "Download data: mtcars",
button_type = "success"
)vector_example <- 1:10
linear_model <- lm(mpg ~ gear, data = mtcars)
list(mtcars, iris, vector_example, linear_model) %>%
downloadthis::download_this(
output_name = "datasets, vector, and linear model",
output_extension = ".rds",
button_label = "Download data as rds",
button_type = "success"
)3.7.2.2 Web address
downloadthis::download_link(
link = "https://github.com/fmmattioni/downloadthis/raw/master/inst/example/file_1.pdf",
button_label = "Download online pdf",
button_type = "warning",
self_contained = FALSE
)3.7.2.3 Local files and directory
downloadthis::download_file(
path = c("./figure/resource.png", "./figure/resource.pdf"),
output_name = "scatter",
button_label = "Download local files",
button_type = "danger",
self_contained = FALSE
)downloadthis::download_dir(
path = "./figure",
output_name = "figures",
button_label = "Download figure dir",
button_type = "danger",
self_contained = FALSE
)文献题录。↩︎