1 html document

  • 在YAML元数据中指定html_document输出格式
---
title: Habits
author: John Doe
date: March 22, 2005
output: html_document
---
  • 在help文档中查看相关的参数
?rmarkdown::html_document()
html_document(
  toc = FALSE,
  toc_depth = 3,
  toc_float = FALSE,
  number_sections = FALSE,
  anchor_sections = FALSE,
  section_divs = TRUE,
  fig_width = 7,
  fig_height = 5,
  fig_retina = 2,
  fig_caption = TRUE,
  dev = "png",
  df_print = "default",
  code_folding = c("none", "show", "hide"),
  code_download = FALSE,
  self_contained = TRUE,
  theme = "default",
  highlight = "default",
  highlight_downlit = FALSE,
  math_method = "default",
  mathjax = "default",
  template = "default",
  extra_dependencies = NULL,
  css = NULL,
  includes = NULL,
  keep_md = FALSE,
  lib_dir = NULL,
  md_extensions = NULL,
  pandoc_args = NULL,
  ...
)

2 Table of contents

使用toc选项添加一个目录(Table of contents),并使用toc_depth选项指定它的深度。

---
title: "Habits"
output:
  html_document:
    toc: true
    toc_depth: 2#默认为3
---

2.1 Floating TOC

指定toc_float选项来将目录浮动到主文档内容的左侧。即使文档滚动,浮动目录也始终是可见的。

  • collapse(默认为TRUE)控制TOC是否折叠次级标题
  • smooth_scroll(默认为TRUE)控制TOC标题是否随滚动高亮变化
---
title: "Habits"
output:
  html_document:
    toc: true
    toc_float: 
      collapsed: false
      smooth_scroll: false
---

3 Section numbering

使用number_sections选项添加节号:

---
title: "Habits"
output:
  html_document:
    toc: true
    number_sections: true
---

4 Tabbed sections

在母节标题后使用{.tabset}将子节折叠在母节下,并且可以设置折叠样式

  • {.tabset-fade} fade样式
  • {.tabet-pills} pills样式
## Quarterly Results {.tabset .tabset-fade .tabset-pills}

### By Product

(tab content)

### By Region

(tab content)
Traditional tabs (upper) and pill (bottom) tabs on an HTML page.

Figure 4.1: Traditional tabs (upper) and pill (bottom) tabs on an HTML page.

5 Appearance and style

  • theme指定主题(Bootswatch主题库)。如果为null,则可以使用css参数添加自己的样式,有效的主题包括:
    • default
    • bootstrap
    • cerulean
    • cosmo
    • darkly
    • flatly
    • journal
    • lumen
    • paper
    • readable
    • sandstone
    • simplex
    • spacelab
    • united
    • yeti
  • highlight指定语法高亮显示样式,输入null阻止高亮。
    • default
    • tango
    • pygments
    • kate
    • monochrome
    • espresso
    • zenburn
    • haddock
    • breezedark
    • textmate
  • smart指示是否生成排版正确的输出,将直引号转换为花引号转换为-破折号,转换为-破折号等。注意,默认情况下smart是启用的。
---
title: "Habits"
output:
  html_document:
    theme: united
    highlight: tango
---

5.1 自定义CSS

使用自定义的css文件时要设置themehighlightnull

---
title: "Habits"
output:
  html_document:
    theme: united
    highlight: tango
---

6 Figure options

  • fig_widthfig_height可以用来控制图形的宽度和高度(默认使用7x5)
  • fig_retina指定缩放(默认为2,目前适用于所有广泛使用的视网膜显示)。设置为null以防止缩放
  • fig_caption控制图形是否带有标题
  • dev控制用于呈现图形的图形设备(默认为png)。
---
title: "Habits"
output:
  html_document:
    fig_width: 7
    fig_height: 6
    fig_caption: true
---

7 Data frame printing

  • 通过df_print选项设置dataframe的默认显示
    • default:调用print.data.frame
    • kable:调用knitr::kable
    • tibble:调用tibble::print.tbl_df
    • paged:调用rmarkdown::paged_table
---
title: "Motor Trend Car Road Tests"
output:
  html_document:
    df_print: paged
---
  • df_print选项被设置为paged时,表将被打印为支持行和列分页的HTML表。可以设置打印参数:
    • max.print 总行数
    • rows.print 每页的行数
    • cols.print 每页的列数
    • cols.min.print 最少的打印列数
    • pages.print 页数
    • paged.print 设置为FALSE则关闭paged table
    • rownames.print 设置为FALSE则关闭行名输出
mtcars
  • A custom function:render的时候调用函数
rmarkdown::html_document(df_print = knitr::kable)
rmarkdown::html_document(df_print = "kable")
#上述两种形式都可以

8 Code folding

code_folding: hide选项允许您包含R代码,但默认情况下将其隐藏。

---
title: "Habits"
output:
  html_document:
    code_folding: hide
---

9 MathJax equations

默认情况下,HTML文档中包含MathJax脚本,用于呈现LaTeX和MathML公式。以使用mathjax选项来控制如何包含mathjax:

  • default 使用来自CDN主机的HTTPS URL(目前由RStudio提供)。
  • local 使用MathJax的本地版本(将其复制到输出目录中)。注意,当使用”local”时,你还需要将self_contained选项设置为false
---
title: "Habits"
output:
  html_document:
    mathjax: local
    self_contained: false
---
  • 指定一个替代URL从另一个位置加载MathJax
---
title: "Habits"
output:
  html_document:
    mathjax: "http://example.com/MathJax.js"
---
  • null 将完全排除MathJax
---
title: "Habits"
output:
  html_document:
    mathjax: null
---

10 Keep Markdown

当knitr处理一个R Markdown输入文件时,它会创建一个Markdown (*.md)文件,随后由Pandoc转换为HTML。如果你想在渲染后保留Markdown文件的副本,你可以使用keep_md选项:

---
title: "Habits"
output:
  html_document:
    keep_md: true
---

11 Include other html

通过包含额外的HTML内容,可以对输出进行更高级的定制。要在文档头或文档正文之前/之后包含内容,可以使用includes选项,如下所示:

---
title: "Habits"
output:
  html_document:
    includes:
      in_header: header.html #头部
      before_body: doc_prefix.html #文档前
      after_body: doc_suffix.html #文档后
---