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
Department of Biostatistics
Vanderbilt University School of Medicine"
date: "`r Sys.Date()`"
output:
html_document:
toc: yes
toc_depth: 3
number_sections: true
toc_float:
-
- 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 will be included in the Hmisc R package soon. For now the function can be sourced in R from github using the following command:
source("https://raw.githubusercontent.com/harrelfe/Hmisc/master/R/hidingTOC.r")
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 here.
tufte_rmarkdown.html