目标是支持从数据表下载所有数据,即使在server = TRUE时也是如此。多亏了Github上的this post,我已经非常接近了。
这是可行的:
library(shiny)
library(DT)
callback <- JS(
"var a = document.createElement('a');",
"$(a).addClass('dt-button');",
"a.href = document.getElementById('download1').href;",
"a.download = '';",
"$(a).attr('target', '_blank');",
"$(a).text('Download');",
"$('div.dwnld').append(a);",
"$('#download1').hide();"
)
ui <- basicPage(
downloadButton("download1", ""), # no label: this button will be hidden
numericInput("nrows", "Number of rows", 10),
DTOutput("dtable")
)
server <- function(input, output, session){
output$dtable <- renderDT(
datatable(iris[1:input$nrows,],
callback = callback,
extensions = 'Buttons',
options = list(
dom = 'B<"dwnld">frtip',
buttons = list(
"copy"
)
)
)
)
output$download1 <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(iris, file)
}
)
}
shinyApp(ui, server)问题是,下载按钮在应用程序加载时会短暂可见。如何确保下载按钮始终不可见?
我尝试使用shinyjs::hidden(),但它导致下载失败:
library(shiny)
library(shinyjs)
library(DT)
callback <- JS(
"var a = document.createElement('a');",
"$(a).addClass('dt-button');",
"a.href = document.getElementById('download1').href;",
"a.download = '';",
"$(a).attr('target', '_blank');",
"$(a).text('Download');",
"$('div.dwnld').append(a);",
"$('#download1').hide();"
)
ui <- basicPage(
useShinyjs(),
hidden(downloadButton("download1", "")), # no label: this button will be hidden
numericInput("nrows", "Number of rows", 10),
DTOutput("dtable")
)
server <- function(input, output, session){
output$dtable <- renderDT(
datatable(iris[1:input$nrows,],
callback = callback,
extensions = 'Buttons',
options = list(
dom = 'B<"dwnld">frtip',
buttons = list(
"copy"
)
)
)
)
output$download1 <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(iris, file)
}
)
}
shinyApp(ui, server)编辑
使用div(style = 'display: none;', ...)也会导致下载失败。
library(shiny)
library(DT)
callback <- JS(
"var a = document.createElement('a');",
"$(a).addClass('dt-button');",
"a.href = document.getElementById('download1').href;",
"a.download = '';",
"$(a).attr('target', '_blank');",
"$(a).text('Download');",
"$('div.dwnld').append(a);",
"$('#download1').hide();"
)
ui <- basicPage(
div(style = "display: none;", downloadButton("download1", "")), # no label: this button will be hidden
numericInput("nrows", "Number of rows", 10),
DTOutput("dtable")
)
server <- function(input, output, session){
output$dtable <- renderDT(
datatable(iris[1:input$nrows,],
callback = callback,
extensions = 'Buttons',
options = list(
dom = 'B<"dwnld">frtip',
buttons = list(
"copy"
)
)
)
)
output$download1 <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(iris, file)
}
)
}
shinyApp(ui, server)发布于 2021-08-09 17:25:51
您可以在downloadButton中使用hidden样式,如下所示。
library(shiny)
library(shinyjs)
library(DT)
callback <- JS(
"var a = document.createElement('a');",
"$(a).addClass('dt-button');",
"a.href = document.getElementById('download1').href;",
"a.download = '';",
"$(a).attr('target', '_blank');",
"$(a).text('Download');",
"$('div.dwnld').append(a);",
"$('#download1').hide();"
)
ui <- basicPage(
# useShinyjs(),
# hidden(downloadButton("download1", "")), # no label: this button will be hidden
downloadButton("download1", "", style = "visibility: hidden;"),
numericInput("nrows", "Number of rows", 10),
DTOutput("dtable")
)
server <- function(input, output, session){
output$dtable <- renderDT(
datatable(iris[1:input$nrows,],
callback = callback,
extensions = 'Buttons',
options = list(
dom = 'B<"dwnld">frtip',
buttons = list(
"copy"
)
)
)
)
output$download1 <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(iris, file)
}
)
}
shinyApp(ui, server)发布于 2021-08-07 06:09:06
将按钮包含在不可见的div中:
ui <- basicPage(
div(
style = "display: none;",
downloadButton("download1", ""), # no label: this button will be hidden
),
numericInput("nrows", "Number of rows", 10),
DTOutput("dtable")
)https://stackoverflow.com/questions/68688737
复制相似问题