R - 下载过滤后的数据表

R - Download Filtered Datatable

我希望能够在使用内置搜索过滤后下载数据表。或者能够使用数据表中使用的相同类型的搜索来过滤数据帧并访问数据表上的搜索。

如果您使用客户端处理,您可以使用输入对象 input[["tablename_rows_all"]] 来完成此操作。 (将 _rows_all 附加到数据表输出槽的名称)

_rows_all 对象将 return 数据框的行索引。下载开始时,您可以在 downloadHandler 中使用它来对数据框进行子集化。

library(shiny)
library(DT)

shinyApp(
  ui = 
    shinyUI(
      fluidPage(
        DT::dataTableOutput("dt"),

        p("Notice that the 'rows_all' attribute grabs the row indices of the data."),
        verbatimTextOutput("filtered_row"),


        downloadButton(outputId = "download_filtered",
                       label = "Download Filtered Data")
      )
    ),

  server = 
    shinyServer(function(input, output, session){
      output$dt <- 
        DT::renderDataTable(
          datatable(mtcars,
                    filter = "top"),
          server = FALSE
        )

      output$filtered_row <- 
        renderPrint({
          input[["dt_rows_all"]]
        })


      output$download_filtered <- 
        downloadHandler(
          filename = "Filtered Data.csv",
          content = function(file){
            write.csv(mtcars[input[["dt_rows_all"]], ],
                      file)
          }
        )
    })
)