2017-02-16 47 views
0

我開發了一個Shiny Dashboard,我有幾個數據框可以通過被動文件讀取器導入等。我還添加了一個「Generate PDF」按鈕,使用downloadButton )在我的ui.R代碼中。我的server.R代碼實現了downloadHandler()來處理這個請求。Shiny downloadButton()和downloadHandler()500錯誤

在我的Windows桌面上,這一切都很完美。我希望它能在我設置的Linux服務器上運行。當然,我必須修改一些路徑,並且Shiny Server在這個盒子上以root身份運行。當我在Linux服務器上運行的網站上單擊「生成PDF」按鈕時,我幾乎立即就會收到HTTP 500錯誤。我自己手動編譯了Linux服務器上的pdfReport.Rmd文件,它運行得很好。

我猜測兩件事情之一:

  1. 不知何故數據沒有獲得通過在Linux中相同的方式,它在Windows桌面上。這可能不太可能,但這是一種可能性。
  2. 我的路徑有問題,所以當臨時文件被寫入開始生成PDF時,系統沒有能力或路徑不存在寫入文件。可能我的downloadHandler()代碼在某種程度上是格式錯誤的。我認爲這比第一名更有可能。

這裏是我的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服務器,它就不能正常工作。任何幫助將非常感激。提前致謝!

回答

0

好 - 多故障診斷之後,我想通了,一些我在閃亮的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()) 
     ) 
     } 
    ) 

    }) 

通知書的,而不是將文件複製到一個臨時目錄,我只是指定爲當前工作目錄中的文件。