HTML documents in Rmarkdown
2023-04-06
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
---
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)
Figure 4.1: Traditional tabs (upper) and pill (bottom) tabs on an HTML page.
5 Appearance and style
theme指定主题(Bootswatch主题库)。如果为null,则可以使用css参数添加自己的样式,有效的主题包括:defaultbootstrapceruleancosmodarklyflatlyjournallumenpaperreadablesandstonesimplexspacelabunitedyeti
highlight指定语法高亮显示样式,输入null阻止高亮。defaulttangopygmentskatemonochromeespressozenburnhaddockbreezedarktextmate
smart指示是否生成排版正确的输出,将直引号转换为花引号,—转换为-破折号,—转换为-破折号等。注意,默认情况下smart是启用的。
---
title: "Habits"
output:
html_document:
theme: united
highlight: tango
---
6 Figure options
fig_width和fig_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.framekable:调用knitr::kabletibble:调用tibble::print.tbl_dfpaged:调用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 tablerownames.print设置为FALSE则关闭行名输出
mtcarsA 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
---