I tried using the syntax of the codes provided here Download filtered data from renderDataTable() in Shiny and here R - Download Filtered Datatable. In my case I'm using an own .csv file and not the standard 'mtcars' data. For some reason I'm not able to find the file if want to download it (I open it in the Browser). The code is as follows:
library(shiny)
library(ggplot2)
library(DT)
library(readr)
tbl <- read.csv(file.choose(new = FALSE), header = TRUE, sep = ",", stringsAsFactors=TRUE)
# Define UI -------
ui <- navbarPage(
title = "Data Table Options",
tabPanel("Lot Dataset",
DT::dataTableOutput("dt"), #datatable
div(h3("Download"), style = "color:blue"),
helpText(" Select the download format"),
radioButtons("type", "Format type:",
choices = c("Excel (CSV)", "Text (Space Separated)", "Doc")),
br(),
helpText(" Click on the download button to download the Lot Dataset"),
p("Below are the row indices of the data."),
verbatimTextOutput("filtered_row"),
br(),
helpText(" Click on the download button to download the Lot Dataset"),
downloadButton("download_filtered", "Download Filtered Data"),
br()
)
)
and the server function with the downloadhandler:
server <- function(input, output) {
thedata <- reactive({datatable(tbl, filter = "top",options = list(pageLength = 25))})
output$dt <- DT::renderDataTable({
thedata()
})
#bottom panel with row indices
output$filtered_row <-
renderPrint({
input[["dt_rows_all"]]
})
#file extension for download
fileext <- reactive({
switch(input$type,
"Excel (CSV)" = "csv", "Text" = "txt", "Doc" = "doc")
})
#downloadHandler() for file download of Lot Dataset
output$download_filtered <- downloadHandler(
filename = function() {
file_name <- paste("MLdataset_test", fileext(), sep=".") #filename
},
content = function(file) {
write.csv(thedata()[input[["dt_rows_all"]], ],
file)
}
)
}
# Run the app ----
shinyApp(ui = ui, server = server)
I want to be able to download the filtered datatable but for some reason it can't find the file when I want to download
Every time I try to download it the following error appears in the console:
Warning: Error in [: incorrect number of dimensions
[No stack trace available]
the dimension of the .csv file ist:
dim(tbl) [1] 19100 56
I would really appreciate any help, been trying to fix this for hours without any success!