【问题标题】:R Shiny - Generating reportR Shiny - 生成报告
【发布时间】:2017-08-24 18:51:14
【问题描述】:

我想从闪亮的应用程序中导出 html/pdf 文件,我发现了这个 https://shiny.rstudio.com/articles/generating-reports.html.

然后我尝试使用该网站的代码,显示如下:

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)

但是,我无法下载报告(Failed-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 服务器。对这个错误有什么建议吗?

提前致谢。

【问题讨论】:

  • 看起来file.copy 没有将您的报告复制到临时文件夹。确保报告在当前目录print(getwd())
  • 感谢@HubertL,我在当前目录中创建了一个空白报告文件,它可以工作。但是,我下载的报告仍然是空白报告。似乎覆盖参数不起作用?谢谢!
  • 我不认为我理解您的问题:呈现空白报告生成空白报告对我来说听起来像是预期的行为
  • @HubertL 其实我不认为我呈现了一份空白报告。正如教程所说,这个应用程序接受一个输入值,并将其作为参数传递给 .Rmd 文档,该文档被渲染(或编织)到 HTML 输出文件中。我也试过这个shiny.rstudio.com/gallery/generating-reports.html(与我发布的代码相同),下载的报告不是空白报告。相反,它返回参数和绘图,这是我的目标。
  • 那么你为什么在之前的评论中说:我在当前目录中创建了一个空白报告文件?

标签: r shiny


【解决方案1】:

要在网站上重新创建示例,您的工作目录中需要存在一个基本的.Rmd(名为report.Rmd,最好创建为R 脚本,以避免不可见的.txt 分机)文件。这个shell文件的内容在问题中提到的网页上给出。所以步骤是:

  1. 使用所述网页中的内容创建一个文件report.Rmd。确保它没有 .txt 扩展名。

  2. 将其放在 R 会话的工作目录中。

  3. 按原样运行代码。

【讨论】:

    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 2011-03-02
    • 2023-03-21
    • 2021-05-20
    • 2016-08-20
    • 2011-04-21
    • 1970-01-01
    相关资源
    最近更新 更多