【问题标题】:How to use downloadButton and downloadHandler inside a shiny module?如何在闪亮的模块中使用 downloadButton 和 downloadHandler
【发布时间】:2022-01-26 05:07:59
【问题描述】:

我正在尝试构建一个闪亮的模块,我可以使用它从闪亮的应用程序下载不同的文件。但是 downloadButton 并没有像我希望的那样工作。它正在响应一个不是我想要的 html 文件。这是我的代码:

library(shiny)

downloadUI <- function(id, label){
  ns <- NS(id)
  
  actionButton(
    inputId = ns("action"),
    label = label,
    icon = icon("download")
  )
}

downloadServer <- function(id, filename){
  moduleServer(
    id,
    function(input, output, session){
      observeEvent(
        input$action,
        {
          showModal(
            modalDialog(
              title = NULL,
              h3("Download the file?", style = "text-align: center;"),
              footer = tagList(
                downloadButton(
                  outputId = "download",
                  label = "Yes"
                ),
                modalButton("Cancel")
              ),
              size = "m"
            )
          )
        }
      )
      
      output$download <- downloadHandler(
        filename = paste0(filename, ".csv"),
        content = function(file){
          write.csv(iris, file = file, row.names = FALSE)
        }
      )
    }
  )
}

ui <- fluidPage(
  downloadUI("irisDownload", label = "Download Iris data")
)

server <- function(input, output, session) {
  downloadServer("irisDownload", filename = "iris")
}

shinyApp(ui, server)

谁能帮我理解我在这里做错了什么?

【问题讨论】:

    标签: r shiny module shinyapps


    【解决方案1】:

    您只需要在服务器端为downloadButton 提供一个命名空间ns。试试这个

    library(shiny)
    
    downloadUI <- function(id, label){
      ns <- NS(id)
      
      actionButton(
        inputId = ns("action"),
        label = label,
        icon = icon("download")
      )
    }
    
    downloadServer <- function(id, filename){
      moduleServer(
        id,
        function(input, output, session){
          ns <- session$ns
          observeEvent(
            input$action,
            {
              showModal(
                modalDialog(
                  title = NULL,
                  h3("Download the file?", style = "text-align: center;"),
                  footer = tagList(
                    downloadButton(
                      outputId = ns("download"),
                      label = "Yes"
                    ),
                    modalButton("Cancel")
                  ),
                  size = "m"
                )
              )
            }
          )
          
          output$download <- downloadHandler(
            filename = paste0(filename, ".csv"),
            content = function(file){
              write.csv(iris, file = file, row.names = FALSE)
            }
          )
        }
      )
    }
    
    ui <- fluidPage(
      downloadUI("irisDownload", label = "Download Iris data")
    )
    
    server <- function(input, output, session) {
      downloadServer("irisDownload", filename = "iris")
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 非常感谢。这解决了问题。
    猜你喜欢
    • 2017-07-06
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2020-01-18
    • 2021-01-10
    • 2020-04-20
    • 2021-10-12
    相关资源
    最近更新 更多