简体   繁体   English

具有Shiny过滤数据的Downloadhandler

[英]Downloadhandler with filtered data in Shiny

I tried using the syntax of the codes provided here Download filtered data from renderDataTable() in Shiny and here R - Download Filtered Datatable . 我尝试使用此处提供的代码语法, 在Shiny中从renderDataTable()下载过滤的数据,在此处R-下载过滤的数据表 In my case I'm using an own .csv file and not the standard 'mtcars' data. 就我而言,我使用的是自己的.csv文件,而不是标准的“ mtcars”数据。 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: 以及带有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: .csv文件的尺寸ist:

dim(tbl) [1] 19100 56 dim(tbl)[1] 19100 56

I would really appreciate any help, been trying to fix this for hours without any success! 我真的很感谢您的帮助,一直试图解决这个问题,但没有成功!

Nice app. 不错的应用程序。 Your issue is mainly that thedata() is a DT::datatable and not the actual data. 您的问题主要是thedata()DT::datatable而不是实际数据。 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)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM