【问题标题】:Saving rmarkdown output locally from Shiny从 Shiny 本地保存 rmarkdown 输出
【发布时间】:2018-04-12 04:35:22
【问题描述】:

我有一个闪亮的应用程序,它使用rmarkdown 生成可下载的报告,效果很好。每次用户上传数据以生成报告时,我希望将报告的副本保存在闪亮的主机上。理想情况下,我想在 downloadHandler 调用中执行此操作,这样我就不必生成两次报告。

一个最小的例子(改编自this shiny article):

library(shiny)

shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    downloadButton("report", "Generate 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())
        )
      }
    )
  }
)

rmarkdown 文件

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

  ```{r}
# The `params` object is available in the document.
params$n
```

A plot of `params$n` random points.

```{r}
plot(rnorm(params$n), rnorm(params$n))
```

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您基本上只需将生成的报告复制到另一个目录即可。这是基于您提供的代码的示例。

    library(shiny)
    
    if(!dir.exists("reportDir"))
       dir.create("reportDir")
    
    shinyApp(
      ui = fluidPage(
        sliderInput("slider", "Slider", 1, 100, 50),
        downloadButton("report", "Generate 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())
            )
    
            # copy generated report
            file.copy(file, paste("reportDir/", Sys.time(), ".html"))
          }
        )
      }
    )
    

    【讨论】:

      猜你喜欢
      • 2014-10-04
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 2021-06-19
      • 2020-06-05
      • 2016-01-05
      相关资源
      最近更新 更多