节选自 Pimp my RMD: a few tips for R Markdown https://holtzy.github.io/Pimp-my-rmd
在 YAML 文件中用 number_sections: TRUE
参数设置自动编号:
---
title: "Your title"
output:
html_document:
number_sections: TRUE
---
# Title
## A subtitle
## Another subtitle
# Another title
可以用 html 中的 <br>
标签来插入换行符。
A first sentence
<br><br><br><br>
A seconde sentence
同样可以用 html 代码居中图片
<center>
![FigName](logo_r_graph_gallery.jpg)
</center>
有时候 R 代码生成的图片周围有太多空白,这时可以用 fig.asp
参数来调整。这里我使用了 fig.asp=0.50
。
library(png)
library(grid)
img <- readPNG("kan.png")
grid.raster(img)
上图为未加参数,下图为添加了参数
我们也可以在文档的开头或结尾添加一些 html 代码。下面是我在本文末尾添加的 html 代码:
<hr />
<p style="text-align: center;">A work by <a href="https://github.com/holtzy/">Yan Holtz</a></p>
<p style="text-align: center;"><span style="color: #808080;"><em>Yan.holtz.data@gmail.com</em></span></p>
<!-- Add icon library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Add font awesome icons -->
<p style="text-align: center;">
<a href="https://twitter.com/r_graph_gallery?lang=en" class="fa fa-twitter"></a>
<a href="https://www.linkedin.com/in/yan-holtz-2477534a/" class="fa fa-linkedin"></a>
<a href="https://github.com/holtzy/" class="fa fa-github"></a>
</p>
将这段 html 代码保存为 footer.html
,然后在 YAML 中声明:
---
title: "Your title"
output:
html_document:
includes:
after_body: footer.html
---
你的 footer 就会变成这样:
除了在每个标题前加上 <br>
外,更方便的办法是直接在 .css
文件中添加样式。创建一个 style.css
文件:
h1, .h1, h2, .h2, h3, .h3 {
margin-top: 84px;
}
在 YAML 中指定该 .css
:
---
title: "A document with a CSS included"
output:
html_document:
css: style.css
---
A title will follow, but with a lot of space before it
# Title 1
content of part 1
# Title 2
content of part 2
我们可以在代码块的 header 中添加图注,例如:
{r, fig.align="center", fig.width=6, fig.height=6, fig.cap="Figure: Here is a really important caption."}
library(tidyverse)
mpg %>%
ggplot( aes(x=reorder(class, hwy), y=hwy, fill=class)) +
geom_boxplot() +
xlab("class") +
theme(legend.position="none")
同样可以用 css
自定义图注样式,比如把下面的代码添加到 style.css
文件中:
<style>
p.caption {
font-size: 0.9em;
font-style: italic;
color: grey;
margin-right: 10%;
margin-left: 10%;
text-align: justify;
}
</style>
图注的样式就会改为这样:
在 R Markdown 中可以用 LaTeX 语法插入数学公式,用 $
分隔 Latex 语法:
$A = (\pi * \lambda \times r^{4}) / \alpha $
在 chunk header 中加上 out.width=c('50%', '50%'), fig.show='hold'
即可:
```{r out.width=c('50%', '50%'), fig.show='hold'}
boxplot(1:10)
plot(rnorm(10))
\```
这个样式同样用 .css
来实现。
首先,在一级标题后加上 {.tabset .tabset-fade .tabset-pills}
(把二级标题作为选项卡)
# Use buttons or tabs for sub-chapters {.tabset .tabset-fade .tabset-pills}
***
Save space in your document using buttons or tabs for sub chapters. Add this code at the end of your title:
## First
A first section
## Second
content of sub-chapter #2
## Third
content of sub-chapter #3
修改 .css
:
.btn {
border-width: 0 0px 0px 0px;
font-weight: normal;
text-transform: ;
}
.btn-default {
color: #2ecc71;
background-color: #ffffff;
border-color: #ffffff;
}
DT 包[1] 可谓是 R Markdown 的最佳伴侣,它可以在网页上轻松实现交互式的表格:
library(DT)
datatable(mtcars, rownames = FALSE, filter="top", options = list(pageLength = 5, scrollX=T) )
有时候你只想分享实验结果而非一些冗长的代码,这时就可以在 YAML 中设置参数来隐藏代码:
output:
html_document:
code_folding: "hide"
同样,我们可以用 css
代码来改变段落的背景色以高亮显示:
<style>
div.blue { background-color:#e6f0ff; border-radius: 5px; padding: 20px;}
</style>
<div class = "blue">
- This is my first conclusion
- This is my second conclusion
</div>
因为 R Markdown可以输出 html 文档,所以理论上可以实现网页中的各种视觉效果。比如实现「视差滚动」:
需要用到的 css
和 header.html
可从这里下载:https://github.com/holtzy/R-Markdown-Parallax
可以在代码块的 header 中添加 cache=TRUE
来缓存特定 chunk,也可以直接在文档的最开始加上 knitr::opts_chunk$set(cache=TRUE)
实现对整个文档的缓存。
注意:请谨慎使用此选项,强烈建议阅读 Yiyi Xie 撰写的有关此部分内容的文档[2]。
为了实现这种效果,我一般用的是 Tim Holman 的代码[3],把这段代码粘贴到 header.html
中,使用方法同添加 footer 代码一样。
您可以使用锚点在 R Markdown 中使用内部链接(可通过锚点连接网页内的任意标题)。如何实现这个效果:
•第一步 - 在标题处添加一个锚点:
# Add a github link in the corner of your document {#github-link}
•第二步 - 用链接指向该锚点:
For example, [this link](#github-link) will bring ...
可使用 plotly
构建交互式图表,充分利用 html 可交互的特性。
library(ggplot2)
library(plotly)
library(gapminder)
p <- gapminder %>%
filter(year==1977) %>%
ggplot( aes(gdpPercap, lifeExp, size = pop, color=continent)) +
geom_point() +
scale_x_log10() +
theme_bw()
ggplotly(p)
你可以使用任何 bootswatch 主题[4]来自定义文档的字体和外观。还可以调整高亮显示样式:
title: "your title"
output:
html_document:
theme: sandstone
highlight: tango
R Markdown 可以使用许多美观的模板,这里推荐两个包:
•prettydoc[5] by Yixuan Qiu•rmdformats[6] by Julien Barnier. It provides several templates, like readthedown
:
按照下面的步骤就可以把你的 html 放在 github 上显示啦:
1.创建 github 账号。2.将 .rmd
文件重命名为 index.rmd
,并 Knit 生成 index.html
文件。3.使用工作目录生成一个 github 仓库4.进入该仓库:Settings -> GitHub Pages -> Source -> Master Branch -> Save5.稍等一会你的 .html
文件就可在相应域名访问。
这一部分内容可参见 blogdown package[7] 。
关于这个话题详细内容参见 Rstudio documentation[8] 。
在文档末尾添加 session info 是一个比较好的习惯,这将提高结果的可重复性。只需一行代码即可实现:
sessionInfo()
## R version 3.4.1 (2017-06-30)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Sierra 10.12.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] bindrcpp_0.2.2 gapminder_0.3.0 plotly_4.7.1 DT_0.4
## [5] forcats_0.3.0 stringr_1.3.1 dplyr_0.7.8 purrr_0.2.5
## [9] readr_1.1.1 tidyr_0.8.2 tibble_1.4.2 ggplot2_3.1.0
## [13] tidyverse_1.2.1 rmarkdown_1.9 epuRate_0.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.0 lubridate_1.7.4 lattice_0.20-35
## [4] assertthat_0.2.0 rprojroot_1.3-2 digest_0.6.18
## [7] psych_1.8.3.3 mime_0.5 R6_2.3.0
## [10] cellranger_1.1.0 plyr_1.8.4 backports_1.1.2
## [13] evaluate_0.10.1 httr_1.3.1 highr_0.6
## [16] pillar_1.2.2 rlang_0.3.0.1 lazyeval_0.2.1
## [19] readxl_1.1.0 rstudioapi_0.7 data.table_1.11.4
## [22] labeling_0.3 foreign_0.8-70 htmlwidgets_1.2.1
## [25] munsell_0.5.0 shiny_1.1.0 broom_0.4.4
## [28] compiler_3.4.1 httpuv_1.4.3 modelr_0.1.1
## [31] pkgconfig_2.0.2 mnormt_1.5-5 htmltools_0.3.6
## [34] tidyselect_0.2.5 viridisLite_0.3.0 crayon_1.3.4
## [37] withr_2.1.2.9000 later_0.7.2 grid_3.4.1
## [40] nlme_3.1-137 jsonlite_1.5 xtable_1.8-2
## [43] gtable_0.2.0 magrittr_1.5 scales_1.0.0.9000
## [46] cli_1.0.0 stringi_1.2.4 reshape2_1.4.3
## [49] promises_1.0.1 xml2_1.2.0 tools_3.4.1
## [52] glue_1.3.0 hms_0.4.2 crosstalk_1.0.0
## [55] parallel_3.4.1 yaml_2.1.19 colorspace_1.3-2
## [58] rvest_0.3.2 knitr_1.20 bindr_0.1.1
## [61] haven_1.1.1
command + Enter on Mac
Ctrl + Enter on Windows
command + Shift + C on Mac
Ctrl + Shift + C on Windows
command + Shift + K on Mac
Ctrl + Shift + K on Windows
command + option + I on Mac (or command + alt + I depending on your keyboard)
Ctrl + ALT + I on Windows
cmd + Shift + A on Mac
Ctrl + Shift + A on Windows
[1]
DT 包: https://rstudio.github.io/DT/
[2]
Yiyi Xie 撰写的有关此部分内容的文档: https://yihui.name/knitr/demo/cache/
[3]
Tim Holman 的代码: https://github.com/tholman/github-corners
[4]
bootswatch 主题: https://bootswatch.com/
[5]
prettydoc: http://yixuan.cos.name/prettydoc/
[6]
rmdformats: https://github.com/juba/rmdformats
[7]
blogdown package: https://github.com/rstudio/blogdown
[8]
Rstudio documentation: https://rmarkdown.rstudio.com/developer_document_templates.html
生信技能树目前已经公开了三个生信知识库,记得来关注哦~
每周文献分享
https://www.yuque.com/biotrainee/weeklypaper
肿瘤外显子分析指南
https://www.yuque.com/biotrainee/wes
生物统计从理论到实践
https://www.yuque.com/biotrainee/biostat