5

I am trying to build a shiny module that I can use to download different files from a shiny app. But the downloadButton is not working as I want them to. It is responding with a html file which is not what I want. Here is my code:

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)

Can anyone help me understand what I am doing wrong here?

1 Answer 1

6

You just need a namespace ns on the server side for the downloadButton. Try this

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.