tycheo
tycheo

Reputation: 37

how to make download button work in R shiny golem module

Taking @Simos Lazarou 's answer to Shiny App Downloads Button Only respond with HTML , how do i make this work in a module in a R shiny golem package? i tried implementing it in a module and ended up downloading a .html file instead of the desired .xlsx file.

how i implemented in a golem package:

app_server.R

#' The application server-side
#'
#' @param input,output,session Internal parameters for {shiny}.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_server <- function( input, output, session ) {
  # List the first level callModules here
  callModule(mod_download_server, "download_ui_1")

}

app_ui.R

#'
#' @param request Internal parameter for `{shiny}`.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_ui <- function(request) {
  tagList(
    # Leave this function for adding external resources
    golem_add_external_resources(),
    # List the first level UI elements here
    fluidPage(
      h1("testapp"),
      mod_download_ui("download_ui_1")
    )
  )
}

mod_download.R (module)

#' download UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_download_ui <- function(id){
  ns <- NS(id)
  tagList(
 downloadButton("downloadData", "Download Metrics Reports")
  )
}

#' download Server Function
#'
#' @noRd
mod_download_server <- function(input, output, session){
  ns <- session$ns
  data_xi <- data.frame(s = c(1:3),r = c(4:6), x =c(19:21))

  output$downloadData <- downloadHandler(

    filename = function(){
      paste(Sys.time(), 'site_mtx.xlsx')
    },

    content = function(file){
      write_xlsx(data_xi, file)
    }
  )
}

Upvotes: 2

Views: 670

Answers (1)

SmokeyShakers
SmokeyShakers

Reputation: 3412

Try attaching the module namespace to the id:

downloadButton(ns("downloadData"), "Download Metrics Reports")

Upvotes: 3

Related Questions