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