---
title: "A Multi-page HTML Document"
author: "Yihui Xie and Romain Lesur"
date: "`r Sys.Date()`"
output:
  pagedown::html_paged:
    toc: true
    # change to true for a self-contained document, but it'll be a litte slower for Pandoc to render
    self_contained: false
lot: true # insert a list of tables
lof: true # insert a list of figures
# uncomment this line to produce HTML and PDF in RStudio:
#knit: pagedown::chrome_print
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Introduction

This is an example of a multi-page HTML document with some options shown in YAML header. See https://pagedown.rbind.io for the full documentation. The rest of this document is random text.

# Random text

```{r, results='asis', echo = FALSE}
random_markdown = function(len = 100) {
  uri = knitr::image_uri(file.path(R.home('doc'), 'html', 'logo.jpg'))
  text = function(len) {
    trimws(paste(sample(c(letters, rep(' ', 10)), len, TRUE), collapse = ''))
  }
  id = function() paste(sample(letters, 8, TRUE), collapse = '')
  figure = function(i = id()) {
    sprintf('![(#fig:%s)The R logo.](%s){width=%d%%}', i, uri, sample(20:60, 1))
  }
  tab = paste(knitr::kable(head(mtcars[, 1:5])), collapse = '\n')
  table = function(i = id()) {
    c(sprintf('Table: (#tab:%s)A table example.', i), tab)
  }
  unlist(lapply(seq_len(len), function(i) {
    if (i %% 20 == 0) return(paste('#', text(sample(10:30, 1))))
    if (i %% 10 == 0) return(paste('##', text(sample(10:30, 1))))
    # insure some elements
    if (i == 3) return(text(50))
    if (i == 4) return(figure("md-fig"))
    if (i == 5) return(text(50))
    if (i == 6) return(table("md-tab"))
    # then random
    type = sample(1:3, 1, prob = c(.9, .03, .07))
    switch(type, text(sample(50:300, 1)), figure(), table())
  }))
}
cat(random_markdown(), sep = '\n\n')
```

# Knitr inserted Figures and tables

## Simple graphics

```{r simple-graphic, fig.cap = 'A very simple plot'}
plot(1)
```

## Simple tables

```{r simple-table}
knitr::kable(head(mtcars, 3), caption = "A Simple table")
```
