Shiny 闪亮的downloadButton()和downloadHandler()错误

Shiny 闪亮的downloadButton()和downloadHandler()错误,shiny,shiny-server,shinydashboard,Shiny,Shiny Server,Shinydashboard,我开发了一个闪亮的仪表板,我有几个数据框,可以通过反应式文件读取器导入,等等。。我还在ui.R代码中使用downloadButton()添加了一个“生成PDF”按钮。我的server.R代码实现downloadHandler()来处理该请求 在我的Windows桌面上,这一切都很完美。我希望它在我安装的Linux服务器上运行。当然,我必须修改一些路径,而闪亮服务器在这个框中以root用户身份运行。当我在Linux服务器上运行的站点上单击“GeneratePDF”按钮时,我几乎立即收到一个HTTP

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

在我的Windows桌面上,这一切都很完美。我希望它在我安装的Linux服务器上运行。当然,我必须修改一些路径,而闪亮服务器在这个框中以root用户身份运行。当我在Linux服务器上运行的站点上单击“GeneratePDF”按钮时,我几乎立即收到一个HTTP500错误。我自己在Linux服务器上手动编译了pdfReport.Rmd文件,它运行得很好

我猜有两件事:

  • 不知何故,数据在Linux设备上的传递方式与在Windows桌面上的不同。这可能不太可能,但有可能
  • 我的路径有问题,因此当写入临时文件以开始生成PDF时,系统没有能力或路径不存在以写入文件。可能我的downloadHandler()代码在某种程度上存在错误。我认为这比#1的可能性更大
  • 以下是我的downloadHandler()代码:


    我开始怀疑这是否是问题所在,我写的不是当前会议的路径或其他什么?这对我来说是一个崭新的领域。正如我所说,在我的桌面上,它可以正常工作,但一旦我将它部署到Linux服务器上,它就不能正常工作。任何帮助都将不胜感激。提前谢谢

    Ok-在进行了大量故障排除之后,我发现我在Shining webroot中拥有的一些文件(它们是主pdfReport.Rmd文件的依赖项)没有被看到,因为代码将报告复制到了一个临时目录

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

    现在我将解决这个问题,因为我已经让它工作了,基本上我的解决方法将是执行以下操作:

    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())
            )
          }
        )
    
      })
    
  • 使服务作为普通用户运行
  • 我将不得不在报告代码中静态引用它们,而不是报告所依赖的文件的副本
  • 我为所有可能读过这篇文章并正在研究这篇文章的人道歉。我对上述代码的修复如下:

    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())
            )
          }
        )
    
      })
    
    输出$pdfReport
    
    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())
            )
          }
        )
    
      })