【问题标题】:Name of downloadable file from Shiny app does not exactly match specification来自 Shiny 应用程序的可下载文件的名称与规范不完全匹配
【发布时间】:2017-09-27 15:27:39
【问题描述】:

我的代码基于this page on R Studio on creating apps with downloadable reports。我创建了以下 app 和 .Rmd 文档,它们都保存在同一个目录中:

app.R:

library(rmarkdown)
library(shiny)

shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    downloadButton("report", "Generate report")
  ),
  server = function(input, output) {
    output$report <- downloadHandler(
      filename = "report.pdf",
      content = function(file) {
        tempReport <- file.path(tempdir(), "report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)

        params <- list(n = input$slider)

        render(tempReport, output_file = file,
           params = params,
           envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

report.Rmd:

---
title: "Dynamic report"
output: pdf_document
params:
  n: NA
---

```{r}
# The `params` object is available in the document.
params$n
```

A plot of `r params$n` random points.

```{r}
plot(rnorm(params$n), rnorm(params$n))
```

当我单击“生成报告”按钮时,保存的文件称为“报告”,而实际上我将其命名为“report.pdf”。我最终不得不手动将“.pdf”添加到文件名中,以便我的计算机将其识别为 PDF 文档。

文件名与我指定的不完全匹配是否有原因?我还能做什么?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    尝试将downloadHandlercontentType 参数设置为正确的MIME 类型,即"application/pdf"

    【讨论】:

    • 下载后我还需要手动在文件名中添加“.pdf”,以便电脑自动查看为PDF。
    • 嗯,它在我的带有 Firefox 和 Edge 的计算机上运行良好。您使用的是哪个浏览器?它可能在 RStudio Viewer 窗格中不起作用,至少我经常遇到问题。
    • 啊啊啊……就是这样。我正在通过 R Studio Viewer 窗格进行尝试。如果我将应用程序转移到浏览器中,它工作正常。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2011-07-31
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多