【问题标题】:Enabling download all with server = TRUE datatable by using invisible download button使用不可见的下载按钮启用全部下载服务器 = TRUE 数据表
【发布时间】:2021-10-11 18:29:27
【问题描述】:

目标是即使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)

【问题讨论】:

  • 试试downloadButton("download1", "", style = "visibility: hidden;")
  • @YBS 这很好用!如果您将此评论转换为答案,我会接受。

标签: r shiny datatables dt


【解决方案1】:

您可以在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)

【讨论】:

    【解决方案2】:

    在不可见的div 中包含按钮:

    ui <- basicPage(
      div(
        style = "display: none;",
        downloadButton("download1", ""), # no label: this button will be hidden
      ),
      numericInput("nrows", "Number of rows", 10),
      DTOutput("dtable")
    )
    

    【讨论】:

    • 请参阅编辑。这可以隐藏下载按钮,但随后下载失败。
    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2017-05-28
    相关资源
    最近更新 更多