1

以@Simos Lazarou 对Shiny App Downloads Button 的回答仅使用 HTML 响应,我如何在 R 闪亮的 golem 包中的模块中使其工作?我尝试在一个模块中实现它并最终下载了一个 .html 文件而不是所需的 .xlsx 文件。

我如何在 golem 包中实现:

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(模块)

#' 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)
    }
  )
}
4

1 回答 1

3

尝试将模块命名空间附加到 id:

downloadButton(ns("downloadData"), "Download Metrics Reports")
于 2021-07-15T12:15:32.260 回答