在 LAN 中使用 shiny 生成可下载的报告

Generating downloadable reports with shiny in LAN

我在 LAN 中与同事的 PC 共享了一个 Rshiny 应用程序,所以我使用了 shiny.hostshiny.port

因此,当我的同事在他们的 PC 上单击 report_button 按钮时尝试下载报告时,他们收到了我在我的 PC 上选择的最后一个值

示例: 在我的电脑上,我选择 input$slider = 46,然后我在 HTML 中下载报告文件,然后我的同事在他的电脑上打开 link 192.168.5.140:8888 在他的浏览器中,选择 input$slider=73 并下载文件,然后 (!!!) 他得到与我得到的相同的报告,即他得到 input$slider = 46

这是代码:

library(shiny)
options(shiny.host = '192.168.5.140')
options(shiny.port = 8888)

ui = fluidPage(
  sliderInput("slider", "Slider", 1, 100, 50),
  downloadButton("report_button", "Generate report") 
)

server = function(input, output) {

  output$report_button<- 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())
         )
       }
     )
    }
shinyApp(ui, server)

和 report.RMD 文件:

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

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

您应该可以通过使用 tempfile 而不是 tempdir 创建临时 Rmd 文件来防止这种情况。

tempdir 是不变的,只要你是 运行 相同的 R-session - 这可能最终会在并发闪亮会话中覆盖彼此的报告结果。

tempfile 然而,为每个函数调用创建一个唯一的文件:

library(shiny)
options(shiny.host = '0.0.0.0')
options(shiny.port = 8888)

ui = fluidPage(
  sliderInput("slider", "Slider", 1, 100, 50),
  downloadButton("report_button", "Generate report") 
)

server = function(input, output) {
  
  output$report_button<- 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 <- tempfile(fileext = ".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())
      )
    }
  )
}
shinyApp(ui, server)