Reputation: 23
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!
Upvotes: 2
Views: 1596
Reputation: 23
This also works (probably too much unnecessary code though)
server <- function(input, output) {
thedata <- reactive({
datatable(tbl, filter = "top",options = list(pageLength = 25))
})
#editing the tbl dataset with the filtered rows only
thedata_filtered <- reactive({
tbl[c(input[["dt_rows_all"]]), ]
})
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.table(thedata_filtered(), file)
}
)
}
Upvotes: 0
Reputation: 3986
Nice app. Your issue is mainly that thedata()
is a DT::datatable
and not the actual data. I've reworked it which now works for me, see comments in the script:
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()
)
)
server <- function(input, output) {
#!! I've moved the datatable directly in here as 'thedata()' was a bit redundant and confusing
output$dt <- DT::renderDataTable({
datatable(tbl,filter = "top",options = list(pageLength = 25))
})
#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() {
paste("MLdataset_test", fileext(), sep=".") #filename
},
content = function(file) {
#!! Use tbl and not 'thedata()' to filter. tbl is the data, the other was the datatable
write.csv(tbl[input[["dt_rows_all"]], ],
file= file,
#!! assumed we don't want the row names
row.names=F)
}
)
}
shinyApp(ui, server)
Upvotes: 1