download filtered data from a dataTable in Shiny

I want to create a button that downloads the filtered data of a dataTable, so I read these two posts and tried to do like them but I got an error and it didnt show the table rows. (please see the


)

R - Download Filtered Datatable and Download filtered data from renderDataTable() in Shiny and Download dataset filtered in Shiny Input

The error is: 'data' must be 2-dimensional (e.g. data frame or matrix) This is a part of my code:
Actually, it couldn't load table rows and show this error. I need to mention when I remove the two last functions, the table works properly and I can see the rows of it.

#UI SECTION
ui <- fluidPage(
downloadButton("download_filtered", "Download filtered dataset"), 
                     
                     verbatimTextOutput("filtered_row"),
                     DT::dataTableOutput("fancyTable"),
                     tags$hr(),
                     plotOutput("fancyPlot")
                              
#SERVER SECTION
 server <- function(input, output) {
  output$fancyTable<- renderDataTable ({
my_data = data_filter()
 DT::datatable(my_data, extensions = "Buttons",
                          options = list(paging = TRUE,
                                         scrollX=TRUE,
                                         searching = TRUE,
                                         ordering = TRUE,
                                         dom = 'l<"sep">Bfrtip',
                                         buttons = c('copy', 'csv', 'excel', 'pdf'),
                                         pageLength=10,
                                         lengthMenu=c(10,20,50,100) )
                         )

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

Since I didn't know what data_filter() was in the code you shared, I set it equal to the first 5 columns of the starwars dataset and made it a reactive. I added "DT::" in front of renderDataTable to stay consistent with the other DT calls in your code, which I assume was done because the DT package was not specifically loaded. I also cleaned up some nesting parentheses issues, which produced the following working app:

Code for the app is as follows:

library(shiny)
library(tidyverse)

#UI SECTION
ui <- fluidPage(
  downloadButton("download_filtered", "Download filtered dataset"), 
  
  verbatimTextOutput("filtered_row"),
  DT::dataTableOutput("fancyTable"),
  tags$hr(),
  plotOutput("fancyPlot")
  )
  
  #SERVER SECTION
  server <- function(input, output) {
    
    data_filter = reactive(starwars[,1:5])
    
    output$fancyTable <- DT::renderDataTable ({
      my_data = data_filter()
      DT::datatable(my_data, 
                    extensions = "Buttons",
                    options = list(paging = TRUE,
                                   scrollX=TRUE,
                                   searching = TRUE,
                                   ordering = TRUE,
                                   dom = 'l<"sep">Bfrtip',
                                   buttons = c('copy', 'csv', 'excel', 'pdf'),
                                   pageLength=10,
                                   lengthMenu=c(10,20,50,100) )
                    )
      })
      
      output$filtered_row <- renderPrint({
        input[["fancyTable_rows_all"]]
      })
      
      output$download_filtered <- downloadHandler(
        filename = "Filtered Data.csv",
        content = function(file){
          write.csv(my_data[input[["fancyTable_rows_all"]], ],
                    file)
        }
      )
      
  }
  
shinyApp(ui = ui, server = server)

Typing "blond" in the search bar and clicking on the CSV button provides this download:

As for the "2-dimensional" error, be sure data_filter is a data frame or matrix and not a vector.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.