如何在 rmarkdown html Shiny 应用程序中添加目录



基于此链接,我尝试在HTML rmarkdown输出中包含目录。如果我只是在 RStudio 中knit它,这工作正常,但是当我在 Shiny 中尝试相同的操作时,目录没有显示。是我做错了什么,还是根本不可能?我也尝试了一些自定义 css,但这似乎也不起作用。我需要这个,因为我的用户需要设置一些输入并使用目录自己下载交互式文档。请在下面找到一个示例。

服务器.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)
    })
})

UIR

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 文档:

---
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.

render中删除html_document()。我没有研究过细节,但看起来它强制覆盖了 yaml 前言。

相关内容

最新更新