如何在rmarkdown html Shiny app中添加目录

时间:2017-01-24 09:00:33

标签: html css r shiny r-markdown

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

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.

1 个答案:

答案 0 :(得分:1)

html_document()移除render。我没有研究过细节,但看起来它强制覆盖了yaml前面的事情。