按钮扩展:下载所有数据或仅可见数据

22
使用DT包的按钮扩展,有没有一种方法可以指定按钮下载(1)馈送数据表的所有数据,或者(2)仅下载可见页面上的数据。
下面是文档中的示例。
datatable(
  iris, extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
  )
)

这些按钮已经可以实现第二个功能了。当你在搜索栏中输入“setosa”时,只需点击复制按钮即可。如果您没有应用任何过滤器,则会获得第一个。所以我不明白这里的问题在哪里。 - 5th
4个回答

29

正如@divibisan所说,一种选择是使用renderDT()server参数来控制下载按钮是仅下载当前行还是全部行。

如果您想要有一个下载按钮,那么这将很好地工作。但是如果您想要始终显示两个按钮,其中一个下载当前页面,另一个下载完整数据集,则可以使用以下代码:

library(shiny)

ui <- fluidPage(
  DT::DTOutput("table")
)

server <- function(input, output, session) {
  output$table <- DT::renderDT(server = FALSE, {
    DT::datatable(
      mtcars,
      extensions = c("Buttons"),
      options = list(
        dom = 'Bfrtip',
        buttons = list(
          list(extend = "csv", text = "Download Current Page", filename = "page",
               exportOptions = list(
                 modifier = list(page = "current")
               )
          ),
          list(extend = "csv", text = "Download Full Results", filename = "data",
               exportOptions = list(
                 modifier = list(page = "all")
               )
          )
        )
      )
    )
  })
}

shinyApp(ui, server)

1
嗨,DeanAttali,这个解决方案在我的端上不起作用。当我点击“下载完整结果”时,仍然只会下载单个页面的数据。有什么想法吗?可能是因为我的是renderDataTable吗?或者是因为我有extensions = c("Buttons","FixedColumns")吗? - Angelo
没有看到你的代码,没有人能回答这个问题。但是你应该尝试复制我在答案中提供的精确代码,以确定你是否做错了什么。如果我提供的精确代码对你来说有不同的行为,那么包中的某些内容就有问题了。 - DeanAttali
嗨@DeanAttali,这个解决方案非常好用,谢谢。但是在使用shiny模块时,它不起作用。不确定,但我猜测这与Buttons扩展的命名空间有关。 - Dhiraj
1
这不是我的经验,我相信它是通过模块工作的。如果您认为它不是这样,请创建一个最小可重现的示例,以显示模块中此功能存在问题,并将其提交为包的问题。 - DeanAttali
3
在模块中也完全可行。我的错,漏掉了server=FALSE这一部分。 - Dhiraj
显示剩余2条评论

16

请参考此答案:按钮:具有滚动条的下载按钮仅下载少量行

按钮导出的数据是全部数据还是仅可见数据,取决于在DT::renderDT函数调用中的server参数。如果server=FALSE,则按钮将导出表格中的所有数据;如果server=TRUE,则它们将只导出可见数据。

您可以使用变量设置server参数,以使其成为可选择的选项。

output$table <- DT::renderDT(server = input$download_all, {
    DT::datatable( ... )
}
另外一个选项是您可能想要查看的exportOptions: modifier: selected选项,它确定是仅下载选定的行(默认)还是所有行。您可以在此处阅读有关该选项的信息:https://datatables.net/extensions/buttons/examples/print/select.html 请注意,如果数据表非常大,使用server=FALSE可能会导致性能和内存问题。

5
你正在寻找“修饰符:页面:已选中”的内容。这里有一个可行的示例。
ui <- fluidPage(
  title = "Examples of DataTables",
  sidebarLayout(
    mainPanel(
      tabsetPanel(
        id = 'dataset',
        tabPanel("diamonds", DT::dataTableOutput("mytable1"))
      )
    )
  )
)

server <- function(input, output) {

  # choose columns to display
  diamonds2 = diamonds[sample(nrow(diamonds), 1000), ]
  output$mytable1 <- DT::renderDataTable({
    DT::datatable(diamonds2,
                  extensions = 'Buttons',
                  options = list(
                    dom = 'Bfrtip',
                    buttons = 
                      list(
                        list(
                        extend = 'csv',
                        buttons = c('csv'),
                        exportOptions = list(
                          modifiers = list(page = "current")
                        )
                      ))
                    )
                  )
  })

}

shinyApp(ui, server)

希望这能帮到您!

2
提供的代码无法运行,我认为即使没有你添加的选项,它也只会打印当前页面。 - DeanAttali
1
我认为 modifiers = list(page = "all") 除非 server = FALSE,否则没有任何有用的作用。正如 @DeanAttali 指出的那样,当 server = TRUE(默认值)时,我不认为 modifiers = list(page = "current") 实际上会改变任何默认行为。 - Giovanni Colitti
这个可行 @Bertil Baron。 - TarJae

4
如果您想在当前页面和整个数据集中包含下载选项,可以将其作为两个下拉按钮实现,其中包括csv或excel文件的选项,以及复制和打印按钮: enter image description here 以下是修改后的可工作代码:
library(shiny)

ui <- fluidPage(
  DT::dataTableOutput("table")
)

server <- function(input, output, session) {
  output$table <- DT::renderDataTable(server = FALSE, {
    DT::datatable(
      mtcars,
      extensions = "Buttons",
      filter = "top",
      selection = "none", #this is to avoid select rows if you click on the rows
      rownames = FALSE,
      
      options = list(
        
        scrollX = TRUE,
        autoWidth = FALSE,
        dom = 'Blrtip', # the important thing is that there is the l to allow for the lengthMenu 
        # https://dev59.com/xK7la4cB1Zd3GeqPkNyF
        
        buttons = list(
          
          # insert buttons with copy and print
          # colvis includes the button to select and view only certain columns in the output table
          # from https://rstudio.github.io/DT/extensions.html 
          I('colvis'), 'copy', 'print',
          
          # code for the first dropdown download button
          # this will download only the current page only (depends on the number of rows selected in the lengthMenu)
          # using modifier = list(page = "current")
          # only the columns visible will be downloaded using the columns:":visible" option from:
          # https://dev59.com/Jbv4oIgBc1ULPQZFnMoh#72317607
          list(
            extend = 'collection',
          buttons = list(
          list(extend = "csv", filename = "page",exportOptions = list(
            columns = ":visible",modifier = list(page = "current"))
          ),
          list(extend = 'excel', filename = "page", title = NULL, 
               exportOptions = list(columns = ":visible",modifier = list(page = "current")))),
          text = 'Download current page'),
          
          # code for the  second dropdown download button
          # this will download the entire dataset using modifier = list(page = "all")
          list(
            extend = 'collection',
            buttons = list(
              list(extend = "csv", filename = "data",exportOptions = list(
                columns = ":visible",modifier = list(page = "all"))
              ),
              list(extend = 'excel', filename = "data", title = NULL, 
                   exportOptions = list(columns = ":visible",modifier = list(page = "all")))),
            text = 'Download all data')
          
        ),
        # add the option to display more rows as a length menu
        lengthMenu = list(c(10, 30, 50, -1),
                          c('10', '30', '50', 'All'))
        ),
      class = "display"
      )
    
  })
}

shinyApp(ui, server)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接