具有Shiny过滤数据的Downloadhandler

时间:2018-11-27 11:52:14

标签: r datatable download shiny

我尝试使用此处Download filtered data from renderDataTable() in Shiny和此处R - Download Filtered Datatable提供的代码的语法。就我而言,我使用的是自己的.csv文件,而不是标准的“ mtcars”数据。 由于某些原因,如果要下载文件,我将找不到该文件(我在浏览器中将其打开)。 代码如下:

{ "head": { "link": [], "vars": ["team", "club"] },
  "results": { "distinct": false, "ordered": true, "bindings": [
    { "team": { "type": "uri", "value": 
 "http://it.dbpedia.org/resource/Modena_Football_Club_1962-1963" }  , 
 "club": { "type": "uri", "value": 
 "http://it.dbpedia.org/resource/Modena_Football_Club" }} ] } }

以及带有downloadhandler的服务器功能:

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

  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)

.csv文件的尺寸ist:

  

dim(tbl)        [1] 19100 56

我真的很感谢您的帮助,一直在努力解决了几个小时而没有成功!

2 个答案:

答案 0 :(得分:0)

不错的应用。您的问题主要是thedata()DT::datatable,而不是实际数据。我已经对其进行了重新设计,现在可以使用,请参见脚本中的注释:

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)

答案 1 :(得分:0)

这也可以(虽然可能有太多不必要的代码)

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