使用不可见的下载按钮启用 server = TRUE 数据表的全部下载

Enabling download all with server = TRUE datatable by using invisible download button

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

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

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

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