【问题标题】:Problem in downloading data using Download Handlers in ShinyApp在 ShinyApp 中使用下载处理程序下载数据时出现问题
【发布时间】:2019-04-22 08:01:04
【问题描述】:

我的 mtcar 原始数据是使用 ShinyApp 中的下载处理程序下载的,而我希望通过处理程序下载 修改后的数据(使用 SelectInputs)。 我也附上了我的代码,请告诉我它们有什么问题。非常感谢:)

library(shiny)
library(tidyr)
library(dplyr)
library(readr)
library(DT)

data_table <- mtcars

# Define UI
ui <- fluidPage(

downloadButton('downLoadFilter',"Download the filtered data"),

selectInput(inputId = "cyl", 
          label = "cyl:",
          choices = c("All",
                      unique(as.character(data_table$cyl))),
          selected = "4", 
          multiple = TRUE),

selectInput(inputId = "vs", 
          label = "vs:",
          choices = c("All",
                      unique(as.character(data_table$vs))),
          selected = "1", 
          multiple = TRUE),

DT::dataTableOutput('ex1'))

server <- function(input, output) {

thedata <- reactive({
if(input$cyl != 'All'){
  return(data_table[data_table$cyl == input$cyl,])
}

else if(input$vs != 'All'){
  return(data_table[data_table$vs == input$vs,])
}

else{
  return(data_table)
}
})

output$ex1 <- DT::renderDataTable(DT::datatable(filter = 'top',
                                              escape = FALSE, 
                                              options = list(pageLength = 
10, scrollX='500px',autoWidth = TRUE),{
                                                thedata() # Call reactive 
thedata()
                                              }))


output$downLoadFilter <- downloadHandler(
filename = function() {
  paste('Filtered data-', Sys.Date(), '.csv', sep = '')
},
content = function(path){
  write_csv(thedata(),path) # Call reactive thedata()
})}

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 原始答案的代码不要删除,其他人以后需要您的问题,如果需要添加第二部分
  • 我将您新问题的代码放在 Vishesh Shrivastav 答案的 cmets 中。

标签: r datatables shiny shiny-server dt


【解决方案1】:

您仅在您的renderDataTable 内更新thedata()。您需要将其设为响应式,然后将其用于呈现为 DataTable 并下载。 将您的服务器更改为:

# Define server logic
server <- function(input, output) {

  thedata <- reactive({
    if(input$cyl != 'All'){
      return(data_table[data_table$cyl == input$cyl,])
    }
    else{
      return(data_table)
    }
  })

output$ex1 <- DT::renderDataTable(DT::datatable(filter = 'top',
                                                  escape = FALSE, 
              options = list(pageLength = 10, scrollX='500px',autoWidth = TRUE),{
                                 thedata() # Call reactive thedata()
                               }))


output$downLoadFilter <- downloadHandler(
    filename = function() {
      paste('Filtered data-', Sys.Date(), '.csv', sep = '')
    },
    content = function(path){
      write_csv(thedata(),path) # Call reactive thedata()
    })}

【讨论】:

  • 非常感谢你,我已经按照你的建议更新了代码(在顶部)。但是,我在使用 else if 语句过滤出 vs column 时遇到了问题。你能看看吗?非常感谢
  • thedata
  • 谢谢老兄,我试过运行它,但是当你输入'cyl=All'然后将'vs'更改为selectInput中的任何值时它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-14
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多