如何使用downloadHandler在闪亮的数据表中创建下载按钮? [英] How to use downloadHandler for the download button create inside shiny datatable?
本文介绍了如何使用downloadHandler在闪亮的数据表中创建下载按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在闪亮的数据表的每一行内创建了下载按钮,如(v0.3.0)
解决方案
它的工作原理如下:
库(发光)
库(DT)
##模块UI
test_data_table_ui<-function(id){
ns<-NS(id)
tagList(
DT :: dataTableOutput(outputId = ns( my_data_table))
)
}
##模块服务器
test_data_table_server<-函数(输入,输出,会话){
ns = session $ ns
myValue<-reactValues(check ='')
ShinyInput<-function(FUN,len,id,ns,...){
输入<-字符(len)
for(i in seq_len(len)){
input [i]<-as.character(FUN(paste0(ns(id),i ),...))
}
输入
}
my_data_table< -active({
tibble :: tibble(
Name = c('Dilbert','Alice','Wally','Ashok','Dogbert '),
动机= c(62、73、3、99、52),
动作= ShinyInput(downloadButton,
5,
'button _',
ns = ns,
标签= Download,
onclick = sprintf( Shiny.setInputValue('%s',this.id),ns( select_button))
)
)
})
lapply(1:5,function(i){
output [[paste0( button _,i)] ]<-downloadHandler(
filename = function(){
paste('data-',Sys.Date(),'.txt',sep ='')
},
内容=函数(文件){
readr :: write_delim(x =虹膜,路径=文件,delim = \t)
}
)
} )
watchEvent(input $ select_button,{
print(input $ select_button)
})
输出$ my_data_table<-DT :: renderDataTable({
datatable(my_data_table(),转义= FALSE,
选项=
list(
preDrawCallback = JS('function(){Shiny.unbindAll(this.api()。table()。node()); }'),
drawCallback = JS('function(){Shiny.bindAll(this.api()。table()。node());}')
)
)
})
}
ui<-fluidPage(
test_data_table_ui(id = test_dt_inside_module)
)
服务器<-函数(输入,输出,会话){
callModule(module = test_data_table_server,id = test_dt_inside_module)
}
ShinyApp(ui,server)
I created download button inside each row of shiny datatable as shown here. The table is inside one of the shiny module. I wonder which id should be attached to output while using downloadHandler
for any of the download button ? I have shown questionable value as XXXXXXXXXXXX
in the code below.
library(shiny)
library(DT)
#>
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#>
#> dataTableOutput, renderDataTable
## module UI
test_data_table_ui <- function(id){
ns <- NS(id)
tagList(
DT::dataTableOutput(outputId = ns("my_data_table"))
)
}
## module server
test_data_table_server <- function(input, output, session ){
ns = session$ns
myValue <- reactiveValues(check = '')
shinyInput <- function(FUN, len, id, ns, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(ns(id), i), ...))
}
inputs
}
my_data_table <- reactive({
tibble::tibble(
Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
Motivation = c(62, 73, 3, 99, 52),
Actions = shinyInput(downloadButton,
5,
'button_',
ns = ns,
label = "Download",
onclick = sprintf("Shiny.setInputValue('%s', this.id)",ns("select_button"))
)
)
})
# output$XXXXXXXXXXXX <- downloadHandler(
# filename = function() {
# paste('data-', Sys.Date(), '.txt', sep='')
# },
# content = function(file) {
# readr::write_delim(x = mpg, path = file, delim = "\t")
# }
# )
observeEvent(input$select_button, {
print(input$select_button)
})
output$my_data_table <- DT::renderDataTable({
return(my_data_table())
}, escape = FALSE)
}
ui <- fluidPage(
test_data_table_ui(id = "test_dt_inside_module")
)
server <- function(input, output, session) {
callModule(module = test_data_table_server , id = "test_dt_inside_module")
}
shinyApp(ui, server)
#>
#> Listening on http://127.0.0.1:3059
Created on 2019-09-17 by the reprex package (v0.3.0)
解决方案
It works like this:
library(shiny)
library(DT)
## module UI
test_data_table_ui <- function(id){
ns <- NS(id)
tagList(
DT::dataTableOutput(outputId = ns("my_data_table"))
)
}
## module server
test_data_table_server <- function(input, output, session ){
ns = session$ns
myValue <- reactiveValues(check = '')
shinyInput <- function(FUN, len, id, ns, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(ns(id), i), ...))
}
inputs
}
my_data_table <- reactive({
tibble::tibble(
Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
Motivation = c(62, 73, 3, 99, 52),
Actions = shinyInput(downloadButton,
5,
'button_',
ns = ns,
label = "Download",
onclick = sprintf("Shiny.setInputValue('%s', this.id)",ns("select_button"))
)
)
})
lapply(1:5, function(i){
output[[paste0("button_",i)]] <- downloadHandler(
filename = function() {
paste('data-', Sys.Date(), '.txt', sep='')
},
content = function(file) {
readr::write_delim(x = iris, path = file, delim = "\t")
}
)
})
observeEvent(input$select_button, {
print(input$select_button)
})
output$my_data_table <- DT::renderDataTable({
datatable(my_data_table(), escape = FALSE,
options =
list(
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
)
)
})
}
ui <- fluidPage(
test_data_table_ui(id = "test_dt_inside_module")
)
server <- function(input, output, session) {
callModule(module = test_data_table_server , id = "test_dt_inside_module")
}
shinyApp(ui, server)
这篇关于如何使用downloadHandler在闪亮的数据表中创建下载按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文