knitr HTML Template | Example html Document | Reproducible Research

Using knitr in conjunction with Rmarkdown and RStudio one can produce nice html reports and material for long presentations where pagination into customary slides becomes tedious. With the RStudio html report format you can also incorporate caching to improve execution time, and include somewhat interactive plotly and other Javascript-based graphics. You can easily produce a floating table of contents. The usual floating TOC in RStudio puts the TOC in the left margin, which uses a lot of real estate and results in a narrower working space. Tom Stewart has adapted the html styles to place the floating TOC in the right margin where it collides less often with graphics and text in the report, and the TOC can be clicked off and on. In addition you can select the heading level the TOC displays using the buttons labeled 1, 2, and 3 under the button that hides the TOC. The TOC can also be placed in the left margin if you so choose using the side argument the the hidingTOC function. The side of the buttons are displayed on can be changed using the buttonSide argument. By default the TOC is displayed on page load. To hide the TOC on page load set the hidden option to TRUE. The depth of the TOC can be adjusted using the levels argument. The levels argument value should match the value found in toc_depth parameter in the documents yaml metadata header.

Here is a recommended template for reports and presentations. You'll see in the hidingTOC line the text you will click on to expand or contract the TOC (here, "Outline" by default "Table of Contents"). To compile into an html document, use knit to html in Rstudio.

title: "Report Title"
author: "Your Name<br><small>Department of Biostatistics<br>Vanderbilt University School of Medicine</small>"
date: "<small>`r Sys.Date()`</small>"
    toc: yes
    toc_depth: 3
    number_sections: true
      collapsed: false
    code_folding: hide
    theme: cerulean
description: "some description ..."
```{r setup,echo=FALSE}
require(Hmisc)    # provides knitrSet and other functions
knitrSet(lang='markdown', fig.path='png/', fig.align='left', w=6.5, h=4.5, cache=TRUE)
# If using blogdown: knitrSet(lang='blogdown')

`r hidingTOC(buttonLabel="Outline")`

The hidingTOC function is in the Hmisc R package. You can see the source code at .

Horizontal Scrolling of R Code

Especially for blogdown articles, it is best to activate a horizontal scrollbar for wide code chunks when viewing on a small screen, instead of line-wrapping. This is simple to do with a small css change, as explained here:

pre code {
  overflow: auto;
  word-wrap: normal;
  white-space: pre;

Tufte html formatting

Another example of report formating is the Tufte style. An example of this can be seen in the file tufte_rmarkdown.html
Topic attachments
I AttachmentSorted ascending Action Size Date Who Comment
stewarttoc.csscss stewarttoc.css manage 0.5 K 02 Jan 2018 - 17:43 FrankHarrell CSS style file for floating TOC in right margin of R html reports
tufte_rmarkdown.htmlhtml tufte_rmarkdown.html manage 1096.5 K 31 Jan 2019 - 12:37 CharlesDupont Example of Tufte document formatting applied to a HTML document.
Topic revision: r12 - 23 Jun 2020, FrankHarrell

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback