【问题标题】:switch in downloadHandler doesn't work R shiny切换 downloadHandler 不起作用 R闪亮
【发布时间】:2018-08-02 09:58:55
【问题描述】:

当我下载报告时,它始终是 PDF 版本。我已经分别测试了每一个,它们都可以工作,所以问题出在开关上,但问题是我尝试了一些东西,但没有任何效果,我不明白为什么。

这是我的代码:

library(rmarkdown)

ui <- fluidPage(
  selectInput("algo", "Selection de l'agorithme utilise pour la simulation:", choice = list("algo1", "algo2")),
  radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'), inline = TRUE),
  downloadButton("report", "Generate report")
)



server <- function(input, output, session) {
  output$report <- downloadHandler(
    filename = switch(input$format,
                      "Word" = function() {
                        paste("reportHtml-", Sys.Date(), ".docx", sep="")
                      }, 
                      "PDF" = function() {
                        paste("reportPDF-", Sys.Date(), ".pdf", sep="")
                      },
                      "Word" = function() {
                        paste("reportHtml-", Sys.Date(), ".html", sep="")
                      }
    ),

    content =  switch(input$format,
                      "Word" = function(file1) {

                        report <- file.path("C:/R_sources/test/reportWord.Rmd")
                        params <- list(n = input$algo)
                        rmarkdown::render(report, output_file = file1,
                                          params = params,
                                          envir = new.env(parent = globalenv())
                        )
                      },

                      "PDF" = function(file2) {

                        report <- file.path("C:/R_sources/test/reportPdf.Rmd")
                        params <- list(n = input$algo)
                        rmarkdown::render(report, output_file = file2,
                                          params = params,
                                          envir = new.env(parent = globalenv())
                        )
                      },

                      "Word" = function(file3) {

                        report <- file.path("C:/R_sources/test/reportWord.Rmd")
                        params <- list(n = input$algo)
                        rmarkdown::render(report, output_file = file3,
                                          params = params,
                                          envir = new.env(parent = globalenv())
                        )
                      }
    )
  )
}

shinyApp(ui, server)

【问题讨论】:

  • 在代码中有两个“Word”大小写,但无论如何它都不起作用。

标签: r download shiny switch-statement


【解决方案1】:

来自https://shiny.rstudio.com/reference/shiny/1.0.5/downloadHandler.html

filename:文件名的字符串...或返回此类字符串的函数。 (可以从此函数中使用反应值和函数。)

因此,您的目标应该是将函数传递给filename 参数。

您的代码中发生的情况是 downloadHelper 在加载时被评估并运行,此时 input$format 设置为“PDF”,因为这是初始值。所以 switch 语句被评估并且filename 总是绑定了 PDF 函数。

您应该做的是设置一个函数,并在其中使用 switch 语句。 content 也是如此。所以应该是这样的:

library(rmarkdown)

ui <- fluidPage(
  selectInput("algo", "Selection de l'agorithme utilise pour la simulation:", choice = list("algo1", "algo2")),
  radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'), inline = TRUE),
  downloadButton("report", "Generate report")
)



server <- function(input, output, session) {
  output$report <- downloadHandler(
    filename = function() {
      switch(input$format,
        Word = paste("reportHtml-", Sys.Date(), ".docx", sep=""), 
        PDF = paste("reportPDF-", Sys.Date(), ".pdf", sep=""),
        HTML = paste("reportHtml-", Sys.Date(), ".html", sep="")
        )
      },

    content = function(file) {
      switch(input$format,
        Word = {
          report <- file.path("C:/R_sources/test/reportWord.Rmd")
          params <- list(n = input$algo)
          rmarkdown::render(report, output_file = file,
                            params = params,
                            envir = new.env(parent = globalenv())
                            )
        },
        PDF = {
          report <- file.path("C:/R_sources/test/reportPdf.Rmd")
          params <- list(n = input$algo)
          rmarkdown::render(report, output_file = file,
                            params = params,
                            envir = new.env(parent = globalenv())
                            )
        },
        HTML = {
          report <- file.path("C:/R_sources/test/reportHtml.Rmd")
          params <- list(n = input$algo)
          rmarkdown::render(report, output_file = file,
                            params = params,
                            envir = new.env(parent = globalenv())
                            )
        }
      )
    }


  )
}

shinyApp(ui, server)

【讨论】:

  • 谢谢,很高兴听到 :) 如果您觉得您的问题得到解答,可以将我的回答标记为已接受
  • 我该怎么做哈哈?
猜你喜欢
  • 2021-12-22
  • 2020-01-25
  • 1970-01-01
  • 1970-01-01
  • 2014-08-24
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
相关资源
最近更新 更多