s.brunel s.brunel - 25 days ago 19
R Question

r shiny download filtrered datatables (DT)

i'm trying to do a

shiny
app to download a filtred
Datatable
:


  • filtered with the
    searsh

  • filtred by deleting line with the
    delete button



(the download part is working as intended)

the problem : when i first filter with the searsh area from Datatable if i delete a line with the button it reset the first filter

my reproductible exemple : edit working solution

library(shinydashboard)
library(DT)
library(shiny)

ui <- dashboardPage(
dashboardHeader(title = "Info boxes"),
dashboardSidebar(),
dashboardBody(
fluidRow(DT::dataTableOutput('data')),
fluidRow(p(class = 'text-center', downloadButton('x3', 'Download Filtered Data')))
)
)

server <- function(input, output) {
df <- reactiveValues(data = data.frame(
Value1 = 1:10,
Value2 = c("A", "B", "C", "D", "E"),
stringsAsFactors = FALSE,
row.names = 1:10
))

output$data <- DT::renderDataTable(
df$data, server = TRUE, filter = 'top', escape = FALSE, selection = 'none')

# download the filtered data
output$x3 = downloadHandler('emergence filtré.csv', content = function(file) {
s = input$data_rows_all
write.table(df$data[s,], file ,sep=";",row.names = F)
})
}
shinyApp(ui = ui, server = server)


Thank you

Answer

I think this is because the search filter is done on the client's side (within the web browser) and it does not really change the underlying data frame. A possible alternative is to add a filter to the table and use that as search function, and also set server side processing. This won't do global searching in one box though.

output$data <- DT::renderDataTable(
  df$data, server = TRUE, filter = 'top', escape = FALSE, selection = 'none'
)