0

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 didn't show the table rows. (please see the attachment)

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

The error is: 'data' must be 2-dimensional (e.g. data frame or matrix) This is a part of my code:

#UI SECTION
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)
                }
              )
})
}

I would be happy if you have any suggestions. Thank you :)

shamimash
  • 17
  • 3
  • It's easier to help you if you provide a minimal [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that we can copy/paste into R to test. The code here is incomplete and it's not clear what all these variables point to. My guess is that `my_data` is a reactive element so you should have something like `write.csv(my_data()[input[["fancyTable_rows_all"]], ],file)` in your `write.csv` – MrFlick Aug 20 '22 at 17:18

1 Answers1

0

I have tweaked the code minimally. First I used mtcars instead of my_data. I think the main issue is to Set server = FALSE in the renderDT function (learned here @Stéphane Laurent) R shiny datatable extension "Buttons" - how to export the whole table to excel?:

library(shiny)
library(DT)

ui <- fluidPage(
  downloadButton("download_filtered", "Download filtered dataset"), 
  
  verbatimTextOutput("filtered_row"),
  DT::dataTableOutput("fancyTable"),
  tags$hr(),
  plotOutput("fancyPlot")  
)

server <- function(input, output, session) {
  output$fancyTable <- 
    DT::renderDT({
      datatable(mtcars, 
                filter = "top",
                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)
                               )
                )
    }, server = FALSE
    )
    
    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, server)

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    Thanks. I found my mistake. I have written the downloading part inside the dataTable function. now my code works. How can I remove the box which shows the number of pages(above the table and below the download button)? my dataset and table are very large so my box has more than 1000 numbers. do yo have any idea? thanks – shamimash Aug 21 '22 at 09:58
  • Just remove this line : `verbatimTextOutput("filtered_row"),` – TarJae Aug 21 '22 at 10:02