R - Download Filtered Datatable

落花浮王杯 提交于 2019-11-28 02:12:19

问题


I would like to be able to download a datatable after it is filtered using it's built in search. Either that or be able to filter a dataframe using the same kind of search used in a datatable and access the search on a datatable.


回答1:


If you use client side processing, you can accomplish this with the input object input[["tablename_rows_all"]]. (append _rows_all to the name of the datatable output slot)

The _rows_all object will return the row indices of your data frame. You can use that within your downloadHandler to subset the data frame when the download is initiated.

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)
          }
        )
    })
)


来源:https://stackoverflow.com/questions/41597062/r-download-filtered-datatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!