PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 27, 2022

[FIXED] how to use downloadButton and downloadHandler inside a shiny module

 November 27, 2022     module, r, shiny, shinyapps     No comments   

Issue

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?


Solution

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)


Answered By - YBS
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing