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.
Asked
Active
Viewed 4,795 times
2
-
what you have tried so far? – Pork Chop Jan 11 '17 at 17:27
-
@PorkChop I've tried looking for a way to access the internal structures of the datatable, and I've tried writing my own search for it, but it just doesn't work as good as the datatable search. The docs show no real way to provide filtering back to the server. – HSchmale Jan 11 '17 at 17:31
-
post the code with your attempts so we can have a look – Pork Chop Jan 11 '17 at 17:34
-
@PorkChop Most of my attempts were reading the documentation. – HSchmale Jan 11 '17 at 17:40
1 Answers
12
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)
}
)
})
)

Benjamin
- 16,897
- 6
- 45
- 65
-
i am trying to download a filtered data table using "d3tablefilter" ( Which allows for multiple strings to be filtered unlike Data table with filtering limitation ) . However replicating your code to download does not work . Can you please help ?? https://github.com/ThomasSiegmund/D3TableFilter – EricA Sep 21 '17 at 12:18