2 RMarkdown基础知识
RMarkdown的设计是为了更容易可重复性,因为计算代码和说明都在同一个文档中,并且结果自动从源代码生成。
RMarkdown支持数十种静态和动态/交互输出格式
下面是一个最小的R标记文档,它应该是一个纯文本文件,扩展名为.Rmd:
# ---
title: "Hello R Markdown"
author: "Awesome Me"
date: "2018-02-14"
output: html_document # 重要
# ---
代码块
fit = lm(dist ~ speed, data = cars)
b = coef(fit)
plot(cars)
abline(fit)
回归的截距是:r b[1]
.
2.1 示例应用
详细内容见**(https://bookdown.org/yihui/rmarkdown/basics-examples.html)**
2.2 编译一个RMarkdown文件
rmarkdown::render("第一章安装.Rm")
You can render it to PDF via:
rmarkdown::render('第一章安装.Rmd')
使用notebook,您可以单独运行代码块,并在RStudio编辑器中查看结果。这是与Rmd文档中的代码进行交互或实验的一种方便的方法,因为您不必编译整个文档。不使用notebook,您仍然可以部分地执行代码块,但是执行只发生在R控制台,并且notebook接口在编辑器的代码块下面显示代码块的结果,这是一个很大的优势。同样,为了可重复性,您需要最终在一个干净的环境中编译整个文档
2.3 速查表
RStudio创建了大量备忘单,包括单页的RMarkdown速查表,这些速查表可以在**(https://www.rstudio.com/resources/cheatsheets/) **
免费获得 。还有一个更详细的RMarkdown参考指南。这两个文档都可以在您熟悉RMarkdown之后用作快速引用。
2.4 RMarkdown输出格式
rmarkdown包中有两种类型的输出格式:文档和演示文稿。所有可用的格式如下:
-
beamer_presentation
-
github_document
-
html_document
-
ioslides_presentation
-
latex_document
-
md_document
-
odt_document
-
pdf_document
-
powerpoint_presentation
-
rtf_document
-
slidy_presentation
-
word_document
对于Rmd文件的YAML元数据中的输出格式名,如果格式来自扩展包,则需要包含包名,
例如,output: tufte::tufte_html
,如果格式来自rmarkdown 包,则不需要使用rmarkdown::
每种输出格式通常都伴随着几个格式选项。所有这些选项都记录在R包帮助页面上。
例如,您可以在R中键入?rmarkdown::html_document
来打开html_document
格式的帮助页面。
当你想使用某些选项时,你必须将值从R转换为YAML,例如,
html_document(toc = TRUE, toc_depth = 2, dev = 'svg')
需要写成
output:
html_document:
toc: true
toc_depth: 2
dev: 'svg'
YAML中的字符串通常不需要引号
除非它们包含特殊字符,例如冒号:。如果您不确定是否应该引用字符串,可以使用yaml包测试它,例如
cat(yaml::as.yaml(list(
title = 'A Wonderful Day',
subtitle = 'hygge: a quality of coziness'
)))
title: A Wonderful Day
subtitle: 'hygge: a quality of coziness'
如果您有需要作为评估R表达式的结果的选项,您可以使用!expr,它告诉yaml包它需要解析和评估这个选项。
下面是一个为HTML输出使用随机主题的例子:
output:
html_document:
theme: !expr sample(c("yeti", "united", "lumen"), 1)
output:
html_document:
toc: true
includes:
in_header: header.html
before_body: before.html
RMarkdown输出格式函数通常有一个pandoc_args
参数,它应该是要传递给Pandoc的额外参数的字符向量。如果您发现任何Pandoc特性没有由输出格式参数表示,您可以使用这个最终参数,例如,
output:
pdf_document:
toc: true
pandoc_args: ["--wrap=none", "--top-level-division=chapter"]
2.5 Markdown语法
可以在**(https://pandoc.org/MANUAL.html)** 上找到Pandoc的Markdown的完整文档。我们强烈建议您至少阅读本页面 一次,以了解Pandoc的Markdown的所有可能性
2.5.1 内联格式
code
, which is rendered as code
.
Hyperlinks are created using the syntax text,
例如: RStudio
# 图像 ![alt text or image title](path/to/image)
Footnotes are put inside the square brackets after a caret 1, 例如: 2.
2.5.2 级别元素
# First-level header
## Second-level header
### Third-level header
如果你不想给某个标题加入文字,可以在标题后加入{-}或者 {.unnumbered},例如:# Preface {-}
您可以通过缩进子列表来将一个列表嵌套到另一个列表中(缩进4),例如,
-
one item
-
one item
-
one item
-
one more item
-
one more item
-
one more item
-
-
the first item
-
the second item
-
the third item
-
one unordered item
-
one unordered item
-
引用的用法,例如
“I thoroughly disapprove of duels. If a man should challenge me,
I would take him kindly and forgivingly by the hand and lead him
to a quiet place and kill him.”— Mark Twain
输出为:
# “I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him.”
# — Mark Twain
一般来说,你最好在相邻但不同的元素之间至少留一行空,例如,标题和段落。这是为了避免Markdown呈现模糊性。如果没有空行,不同风格的RMarkdown可能会产生不同的结果。
2.5.3 数学表达式
内联LaTeX方程可以用LaTeX语法写成一对美元符号,例如: f ( k ) = ( n k ) p k ( 1 − p ) n − k f(k) = {n \choose k} p^{k} (1-p)^{n-k} f(k)=(kn)p