作者:Yihui Xie
    译者:郑宝童
    日期:2021.06.21


    Thanks to the power of R and Pandoc, you can easily do computing in R Markdown documents, and convert them to a variety of output formats, including HTML/PDF/Word documents, HTML5/Beamer slides, dashboards, and websites, etc. An R Markdown document usually consists of the YAML metadata (optional) and the document body. We have introduced the syntax for writing various components of the document body in Chapter 2, and we explain more about the YAML metadata in this section.

    借助 R 和 Pandoc 的强大功能,您可以轻松地在 R Markdown 文档中进行计算,并将其转换为多种输出格式,包括 HTML/PDF/Word 文档、HTML5/Beamer 幻灯片、仪表板和网站等。 R Markdown 文档通常由 YAML 元数据(可选)和文档正文组成。我们已经在第 2 章中介绍了用于编写文档正文的各种组件的语法,并在本节中详细解释了 YAML 元数据。

    Metadata for R Markdown can be written in the very beginning of a document, starting and ending with three dashes ---, respectively. YAML metadata typically consists of tag-value pairs separated by colons, e.g.,

    R Markdown 的元数据可以写在文档的最开头,分别以三个破折号 —- 开始和结束。 YAML 元数据通常由以冒号分隔的tag-value对组成,例如,

    1. ---
    2. title: "An R Markdown Document"
    3. author: "Yihui Xie"
    4. ---

    For character values, you may omit the quotes when the values do not contain special characters, but it is safer to quote them if they are expected to be character values. Besides characters, another common type of values are logical values. Both yes and true mean true, and no/false mean false, e.g.,

    对于字符值,当值不包含特殊字符时,您可以省略引号,但如果期望它们是字符值,则将使用引号更安全。 除了字符,另一种常见的值类型是逻辑值。 yestrue 均表示真,而 no / false 表示假,例如,

    1. link-citations: yes

    Values can be vectors, and there are two ways of writing vectors. The following two ways are equivalent:

    值可以是向量,有两种写向量的方式。以下两种方式是等价的:

    1. output: ["html_document", "word_document"]
    1. output:
    2. - "html_document"
    3. - "word_document"

    Values can also be lists of values. You just need to indent the values by two more spaces, e.g.,

    值也可以是值列表。您只需要将值再缩进两个空格,例如,

    1. output:
    2. bookdown::gitbook:
    3. split_by: "section"
    4. split_bib: no

    It is a common mistake to forget to indent the values. For example, the following data
    忘记缩进值是一个常见的错误。比如下面的数据

    1. output:
    2. html_document:
    3. toc: yes

    actually means

    1. output: null
    2. html_document: null
    3. toc: yes

    instead of what you probably would have expected:

    1. output:
    2. html_document:
    3. toc: yes

    The R Markdown output format is specified in the output field of the YAML metadata, and you need to consult the R help pages for the possible options, e.g., ?rmarkdown::html_document, or ?bookdown::gitbook. The meanings of most other fields in YAML can be found in the Pandoc documentation.

    R Markdown 输出格式在 YAML 元数据的 output 字段中指定,您需要查阅 R 帮助页面以获取可能的选项,例如 ?rmarkdown::html_document?bookdown::gitbook . YAML 中大多数其他字段的含义可以在 Pandoc 文档中找到。
    The rmarkdown package has provided these R Markdown output formats:

    • beamer_presentation
    • context_document
    • github_document
    • html_document
    • ioslides_presentation
    • latex_document
    • md_document
    • odt_document
    • pdf_document
    • powerpoint_presentation
    • rtf_document
    • slidy_presentation
    • word_document

      There are many more possible output formats in other R packages, including bookdown, tufte, rticles, flexdashboard, revealjs, and rmdformats, etc.

    其他 R 包中还有更多可能的输出格式,包括 bookdowntufterticlesflexdashboardrevealjsrmdformats 等。