1

Based on this link I tried to include a table of contents in an HTML rmarkdown output. This works fine if I just knit it in RStudio, but when I try the same in Shiny, the table of contents doesn't show up. Am I doing something wrong or is this simply not possible? I also tried some custom css but that also doesn't seem to work. I need this because my users need to set some inputs and download an interactive document themselves with a toc. Please find an example below.

server.r

library(shiny)
library(rmarkdown)
library(htmltools)
library(knitr)
shinyServer(function(input, output) {

  output$distPlot <- renderPlot({

    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')

  })
  output$Generate_PDF_Document <- downloadHandler(

    # For PDF output, change this to "report.pdf"
    filename = paste0("Highchart Document",format(Sys.Date(),"%d%m%Y"),".html"),
    content = function(file) {
      #   # Copy the report file to a temporary directory before processing it, in
      #   # case we don't have write permissions to the current working dir (which
      #   # can happen when deployed).
      tempReport <- normalizePath('Test.Rmd')
      file.copy(tempReport, "Test.Rmd", overwrite = FALSE)


      # Knit the document, passing in the `params` list, and eval it in a
      # child of the global environment (this isolates the code in the document
      # from the code in this app).
      out <- render('Test.Rmd', html_document())
      file.rename(out,file)
    })

})

ui.r

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot"),
      downloadButton('Generate_PDF_Document','Generate a PDF Report')
    )
  )
))

rmarkdown doc:

---
title: "Test"
output:
  html_document:
    toc: true
    toc_depth: 3
    toc_float: true
---

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Community
  • 1
  • 1
Tim_Utrecht
  • 1,459
  • 6
  • 24
  • 44

1 Answers1

2

Remove html_document() from render. I have not studied the details, but it looks like it forces override of the yaml front matter.

Dieter Menne
  • 10,076
  • 44
  • 67