轻松记录您
灵感和创意

关于R Markdown的一些提示

章节自动编号

在YAML文件中用 number_sections: TRUE 参数设置自动编号:

—title: “Your title”output: html_document: number_sections: TRUE—# Title## A subtitle## Another subtitle# Another title

跳过一行

可以用html中的

标签来插入换行符。

A first sentence

A seconde sentence

图片居中

同样可以用html代码居中图片

减少图片周围的空白区域

有时候R代码生成的图片周围有太多空白,这时可以用 fig.asp 参数来调整。这里我使用了 fig.asp=0.50。

library(png)library(grid)img <- readPNG(“kan.png”)grid.raster(img)

上图为未加参数,下图为添加了参数

添加页脚和页眉

我们也可以在文档的开头或结尾添加一些html代码。下面是我在这里末尾添加的html代码:

将这段html代码保存为 footer.html,然后在YAML中声明:

—title: “Your title”output: html_document: includes: after_body: footer.html—

你的页脚就会变成这样:

在标题前加上空行

除了在每个标题前加上

外,更方便的方法是直接在 .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 1content of part 1# Title 2content 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 文件中:

图注的样式就会改变这样:

数学公式

在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:## FirstA first section## Secondcontent of sub-chapter #2## Thirdcontent 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包来展示表格

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 代码来改变本身的背景色以高亮显示:

– This is my first conclusion- This is my second conclusion

视差滚动

因为R Markdown可以输出html文档,所以理论上可以实现网页中的各种视觉效果。从而实现“视差滚动”:

需要用到的 css 和 header.html 可从这里下载:https://github.com/holtzy/R-Markdown-Parallax

缓存

可以在代码块的标题中添加 cache=TRUE 来缓存特定的块,也可以直接在文档的最开始加上 knitr::opts_chunk$set(cache=TRUE) 实现对整个文档的缓存。

注意:请谨慎使用此选项,强烈建议阅读 Xiyi Xie撰写的有关此部分内容的文档[2]。

在网页右上角加上Github链接

为了实现这种效果,我一般用的是 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] 由艺轩邱• Julien Barnier的rmdformats [6]。它提供了几个模板,例如 readthedown:

把网页分享到互联网上

按照下面的步骤就可以把你的html放在github上显示啦:

1.创建github账号。2.将 .rmd 文件重命名为 index.rmd,并编织生成 index.html 文件。3.使用工作目录生成一个github仓库4.进入该仓库:设置-> GitHub页面->源->主分支->保存5.稍等一会你的 .html 文件就可在相应域名访问。

用R Markdown创建静态网页

这一部分内容可看到 blogdown软件包[7] 。

创建R Markdown模板

关于这个主题的详细内容参见 Rstudio文档[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 MacCtrl + Enter on Windows

插入注释

command + Shift + C on MacCtrl + Shift + C on Windows

编织R Markdown文件

command + Shift + K on MacCtrl + 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 MacCtrl + Shift + A on Windows

未经允许不得转载:坚果云Markdown » 关于R Markdown的一些提示
分享到: 更多 (0)

坚果云Markdown轻松记录您 灵感和创意

坚果云Markdown下载坚果云Markdown介绍