Most of these templates are showcased in a gallery.
Beyond the basics settings (code & plot chunks, code & languages, parameters, tables), we have specific settings and options for HTML documents.
This post is a wrap-up of templates. It covers the prettydoc
package, the rmarkdown
package (GitHub), the rmdformats
package (clear, docco, readthedocs), and the tufte
package.
This is similar to Jupyter Notebook.
The document default settings…
output: document
…can be changed; see the appearance and style.
output:
html_document:
theme: default
highlight: default
output: ioslides_presentation
creates simple HTML slides with smooth transitions in the browser.output: slidy_presentation
creates contrasted HTML slides in the browser.output: beamer_presentation
creates simple PDF slides.The difference between a Document and a Shiny document…
output: html_document
runtime: shiny
… or a Presentation and a Shiny presentation…
output: ioslides_presentation
runtime: shiny
…is the interactivity.
They have standard settings for theme and highlight.
Install the prettydoc
package.
# Document
output:
prettydoc::html_pretty:
theme: architect
highlight: github
# Vignette
output:
prettydoc::html_pretty:
theme: cayman
highlight: github
Install the rmarkdown
package.
# Document
output: github_document
# Vignette
output: rmarkdown::html_vignette
Install the rmdformats
package.
output:
rmdformats::readthedown:
highlight: kate
…rmdformats::html_clean:
and rmdformats::html_docco:
.
Install the tufte
package.
output:
tufte::tufte_handout: default
...
Install the rticles
package.
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
output:
html_document:
toc: true
toc_depth: 2 # 2 shows levels # and ##
toc_float: true # always visible on the left
collapsed: false # sub-section are displayed
smooth_scroll: true
number_sections: true # add numbers
Add {.tabset .tabset-fade .tabset-pills} following a ## Title
.
output:
html_document:
fig_width: 7
fig_height: 6
fig_caption: true
# Markdown
::kable(mtcars[1:3,]) knitr
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
# HTML
library(tibble)
print(as_tibble(mtcars[1:3,]))
## # A tibble: 3 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
For long tables, set the heading…
output:
html_document:
df_print: paged
…and set the code chunk.
{r, cols.print=6, rows.print=3}
mtcars
code_folding: hide
displays the code with the Code button
output:
html_document:
code_folding: hide
code_folding: show
displays the code with the Hide button
code_folding: hide
:
code_folding: code
:
By default, R Markdown produces standalone HTML files with no external dependencies, using data: URIs to incorporate the contents of linked scripts, stylesheets, images, and videos. For dependencies on external files.
output:
html_document:
self_contained: false
For self-contained documents, MathJax is still loaded externally. We can serve MathJax locally: mathjax: local
and self_contained: false
.
Creates a Markdown file with HTML. Or replace html_document
with md_document
. Consult the documentation for Markdown options.
output:
html_document:
keep_md: true
HTML fragments are not complete HTML documents. Rather, they are intended for inclusion within other web pages or content management systems (like blogs). As such, they don’t support features like embedded images, themes, or code highlighting (it’s expected that the environment they are ultimately published within handles these things).
title: "Optional"
output:
html_fragment:
number_sections: true
smart: true
keep_md: true
fig_width: 7
fig_height: 6
fig_caption: true
fig_retina: 2
A vignette is a long-form guide to a package.↩︎