闪亮 - 动态地向rmarkdown报告添加部分

时间:2017-06-23 04:52:52

标签: r shiny r-markdown

每次点击actionButton printNewPlot时,我想添加一个包含新标题和新图表的新报表部分。我怎么能这样做?

app.R

library(igraph)

 shinyApp(
      ui = fluidPage(
        sliderInput("slider", "Slider", 1, 100, 50),
        actionButton("printNewPlot", "Print new plot to report"),
        downloadButton("report", "Download report")
      ),
      server = function(input, output) {
        output$report <- downloadHandler(
          # For PDF output, change this to "report.pdf"
          filename = "report.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 <- file.path(tempdir(), "report.Rmd")
            file.copy("report.Rmd", tempReport, overwrite = TRUE)

            # Set up parameters to pass to Rmd document
            params <- list(n = input$slider)

            # 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).
            rmarkdown::render(tempReport, output_file = file,
                              params = params,
                              envir = new.env(parent = globalenv())
            )
          }
        )
      }
    )

report.Rmd

---
title: "Dynamic report"
output: html_document
params:
  n: NA
---

# I want a new title each time that printNewPlot is clicked

```{r}

plot(make_ring(params$n))
```

# If printNewPlot is clicked again, a new title must appear here

```{r}
#alongside a new plot
plot(make_ring(params$n))
```

1 个答案:

答案 0 :(得分:1)

您需要两件事:一个存储输入列表的对象;在RMD中循环打印,接收存储的输入作为参数。请注意,我没有make_ring()函数,以便通过错误。

对于应用:

server = function(input, output) {

RV <- reactiveValues(Clicks=c())

observeEvent(input$slider, {

  #create object for clicked polygon
  click <- input$slider
  RV$Clicks <-c(RV$Clicks,click)
  print(unique(RV$Clicks))

})

output$report <- downloadHandler(
  # For PDF output, change this to "report.pdf"
  filename = "report.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 <- file.path( "report.Rmd")
    #file.copy("report.Rmd", tempReport, overwrite = TRUE)

    # Set up parameters to pass to Rmd document
    params <- list(n = RV$Clicks)

    # 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).
    rmarkdown::render(tempReport, output_file = file,
                      params = params,
                      envir = new.env(parent = globalenv())
    )
  }
)

} )

对于RMD文件

---
title: "Dynamic report"
output: html_document
params:
  n:  NA
---

```{r grouping_loop, include=TRUE, echo=FALSE, results='asis'}

n <- params$n

for (i in n){
  cat('\n')
  cat("#  ", i, " \n")
  print(
    i
  )
  cat('\n')
  cat('\n')
}

```