Nicolabo
Nicolabo

Reputation: 1377

Toc in html_document wtih shiny does not work

I prepared very simple rmarkdown document with shiny widget. I want to add table of content but although I use toc: yes it doesn't work at all, why?

---
title: "Test document"
author: "XXX"
date: "XXX"
output:
  html_document:
    toc: yes
runtime: shiny
---


```{r, echo = FALSE}
h2('R package')
br()    
h3('Ggplot2')
br()
h4('Mtcars')

library(ggplot2)
data(mtcars)
selectInput('name', 'Choose a cylinder:',
            choices = sort(unique(mtcars$cyl)),
            selected = sort(unique(mtcars$cyl))[1])
data <- reactive(subset(mtcars,cyl == input$name))
number <- reactive(which(sort(unique(mtcars$cyl)) == input$name))
renderPlot(ggplot(data(),aes(qsec, mpg))+
  geom_point(size = 6))
```

Upvotes: 0

Views: 625

Answers (1)

Your headers need to be outside the code chunk for the table of content to catch them.
You should then use the appropriate numbers of # instead of the html style to visualize the headers size.

Note also that the toc depth default to three, so in your case mtcars won't appear in the toc, unless you adjust the toc_depth to 4.

This should do what you want :

---
title: "Test document"
author: "XXX"
date: "XXX"
output:
  html_document:
    toc: TRUE
    toc_depth: 4
runtime: shiny
---

## R package

### ggplot2

#### mtcars

```{r, echo = FALSE}
library(ggplot2)
data(mtcars)
selectInput('name', 'Choose a cylinder:',
            choices = sort(unique(mtcars$cyl)),
            selected = sort(unique(mtcars$cyl))[1])
data <- reactive(subset(mtcars,cyl == input$name))
number <- reactive(which(sort(unique(mtcars$cyl)) == input$name))
renderPlot(ggplot(data(),aes(qsec, mpg))+
            geom_point(size = 6))
```

Side note : even if it's working it's more common to use toc: TRUE rather than toc: yes

Upvotes: 3

Related Questions