如何使下载按钮在 R shiny golem 模块中工作
how to make download button work in R shiny golem module
根据@Simos Lazarou 对 的回答,我如何在 R shiny 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)
}
)
}
尝试将模块命名空间附加到 id:
downloadButton(ns("downloadData"), "Download Metrics Reports")
根据@Simos Lazarou 对
我是如何在 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)
}
)
}
尝试将模块命名空间附加到 id:
downloadButton(ns("downloadData"), "Download Metrics Reports")