【问题标题】:Shiny downloadButton() and downloadHandler() 500 Error闪亮的 downloadButton() 和 downloadHandler() 500 错误
【发布时间】:2017-07-06 01:41:20
【问题描述】:

我开发了一个闪亮的仪表板,我有几个通过反应式文件阅读器导入的数据框等。我还添加了一个“生成 PDF”按钮,在我的 ui.R 代码中使用 downloadButton()。我的 server.R 代码实现了 downloadHandler() 来处理该请求。

在我的 Windows 桌面上,这一切都完美无缺。我希望它在我设置的 Linux 服务器上运行。当然,我不得不修改一些路径,Shiny Server 在这个盒子上以 root 身份运行。当我在 Linux 服务器上运行的站点上单击“生成 PDF”按钮时,我几乎立即收到 HTTP 500 错误。我自己在 Linux 服务器上手动编译了 pdfReport.Rmd 文件,它运行得很好。

我猜两件事之一:

  1. 不知何故,数据在 Linux 机器上的传递方式与在 Windows 桌面上的传递方式不同。这可能不太可能,但有可能。
  2. 我的路径有问题,所以当写入临时文件以开始生成 PDF 时,系统没有能力或不存在写入文件的路径。可能我的 downloadHandler() 代码在某些方面格式不正确。我认为这比 #1 的可能性更高。

这是我的 downloadHandler() 代码:

output$pdfReport <- downloadHandler(
  # For PDF output, change this to "report.pdf"
  filename = reactive({paste0("/srv/shiny-server/itpod/","ITPOD-",Sys.Date(),".pdf")}),

  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("/srv/shiny-server/itpod", "pdfReport.Rmd")
    file.copy("report.Rmd", tempReport, overwrite = TRUE)

    params <- list(ilp=updateILP(), ico=updateICO(), sec=updateSecurity(), ppwc=updateWorkPreviousPeriodCompleted(),
                   pow=updateOngoingWorkCABApproved(), pwcr=updatePlannedWorkCABRequested(), epca=updateEmergencyChangesPendingCABApproval(),
                   fac=updateFacilities(), drs=updateDRStatus(), ov=updateOperationalEvents(), sl=updateStaffLocations(),
                   w = updateWeather())

    # 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())
    )
  }
)

我认为路径可能是不可写的,所以我尝试将其更改为 /tmp,但这也不起作用。翻来覆去,我发现当我点击“生成 PDF”按钮时,我得到一个带有“会话”的长 URL:

http://my.url.com:3838/itpod/session/d661a858f5679aba26692bc9b4442872/download/pdfReport?w=

我开始怀疑这是否是问题所在,并且我没有写入当前会话的路径或其他什么?对于 Shiny 来说,这对我来说是一个新领域。就像我说的,在我的桌面上它工作正常,但是一旦我将它部署到 Linux 服务器,它就不能正常工作。任何帮助将非常感激。提前致谢!

【问题讨论】:

    标签: shiny shiny-server shinydashboard


    【解决方案1】:

    好的 - 经过多次故障排除后,我发现我在闪亮的 webroot 中拥有的一些文件是主 pdfReport.Rmd 文件的依赖项,因为代码将报告复制到临时目录。

    因为我不想将所有文件从我的 webroot 复制到 temp,所以我决定让报告在 webroot 本身内呈现。对我来说,这没什么大不了的,因为我闪亮的应用程序无论如何都是以 root 身份运行的。

    现在我将解决这个问题,基本上我的解决方法是执行以下操作:

    1. 让服务以普通用户身份运行
    2. 我必须在报告代码中静态引用它们,而不是报告依赖的文件的副本。

    对于所有可能已经阅读并正在努力的人,我深表歉意。我的修复是对上面的代码如下:

    output$pdfReport <- downloadHandler(
          # For PDF output, change this to "report.pdf"
          filename = reactive({paste0("/srv/shiny-server/itpod/","ITPOD-",Sys.Date(),".pdf")}),
    
          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).
    
            report <- file.path(getwd(), "pdfReport.Rmd")
            #tempReport <- file.path(tempdir(), "pdfReport.Rmd")
            #file.copy("pdfReport.Rmd", tempReport, overwrite = TRUE)
    
            params <- list(ilp=updateILP(), ico=updateICO(), sec=updateSecurity(), ppwc=updateWorkPreviousPeriodCompleted(),
                           pow=updateOngoingWorkCABApproved(), pwcr=updatePlannedWorkCABRequested(), epca=updateEmergencyChangesPendingCABApproval(),
                           fac=updateFacilities(), drs=updateDRStatus(), ov=updateOperationalEvents(), sl=updateStaffLocations(),
                           w = updateWeather())
    
            # 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(report, output_file = file, params = params, envir = new.env(parent = globalenv())
            )
          }
        )
    
      })
    

    注意,我没有将文件复制到临时目录,而是在当前工作目录中指定文件。

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 2020-01-08
      • 1970-01-01
      • 2020-04-20
      • 2014-08-24
      • 2018-08-02
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多