2017-04-12 148 views
1

我想從閃亮的應用導出html/pdf文件,我發現這個 https://shiny.rstudio.com/articles/generating-reports.htmlR閃亮 - 生成報告

然後我試圖從該網站,該網站顯示的打擊使用代碼:

library(shiny) 

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

ui <- fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50), 
    downloadButton("report", "Generate report")) 

shinyApp(ui = ui, server = server) 

但是,我不能下載的報告(故障服務器的問題),並得到了錯誤:

Listening on http://127.0.0.1:3638 
Warning in normalizePath(path, winslash = winslash, mustWork = mustWork) : 
    path[1]="/tmp/Rtmp1BbjoR/test.Rmd": No such file or directory 
Warning: Error in tools::file_path_as_absolute: file '/tmp/Rtmp1BbjoR/test.Rmd' does not exist 
Stack trace (innermost first): 
    58: tools::file_path_as_absolute 
    57: dirname 
    56: setwd 
    55: rmarkdown::render 
    54: download$func [example_code/report.R#23] 
    5: <Anonymous> 
    4: do.call 
    3: print.shiny.appobj 
    2: print 
    1: print 
Error : file '/tmp/Rtmp1BbjoR/test.Rmd' does not exist 

我正在R服務器上工作。有這個錯誤的建議嗎?

在此先感謝。

+0

它看起來像'file.copy'不會將您的報告複製到臨時文件夾。確保報告在當前目錄'print(getwd())' – HubertL

+0

謝謝@HubertL,我在當前目錄中創建了一個空白報告文件,它可以工作。但是,我下載的報告仍然是空白報告。看來覆蓋參數不起作用?謝謝! – hualahualaqqq

+0

我不認爲我理解你的問題:渲染一個空白報告產生一個空白報告聽起來像我預期的行爲 – HubertL

回答

0

要重新創建示例網站上,骨骼.Rmd(命名爲report.Rmd,優選作爲R腳本創建,以避免無形.txt分機)文件需要存在於你的工作目錄。這個shell文件的內容在問題中提到的網頁上給出。因此,步驟是:

  1. 創建一個文件,report.Rmd,從規定的網頁內容。確保它沒有.txt擴展名。

  2. 將它放在R會話的工作目錄中。

  3. 按原樣運行代碼。