Shining downloadHandler-Won';t下载屏幕上显示的过滤数据

Shining downloadHandler-Won';t下载屏幕上显示的过滤数据,r,shiny,dt,R,Shiny,Dt,我正在尝试将如下所示的相同基本(可复制)代码应用于我的应用程序,如本测试示例中提供的(并使用提供的解决方案)所示: 我对它进行了一些定制,以便表使用selectInput()过滤器,而不是在呈现时显示在数据表顶部的过滤器 问题在于,在引用链接中的示例代码中,以及在我的例子中,过滤器都应该应用于屏幕上的datatable,但当通过downloadHandler(),时,它不会导出过滤后的数据。它过滤掉已过滤的正确行,但不是正确的数据 我正在努力想出一个解决方案,在下载文件时将我想要的“反应式”应用

我正在尝试将如下所示的相同基本(可复制)代码应用于我的应用程序,如本测试示例中提供的(并使用提供的解决方案)所示:

我对它进行了一些定制,以便表使用
selectInput()
过滤器,而不是在呈现时显示在数据表顶部的过滤器

问题在于,在引用链接中的示例代码中,以及在我的例子中,过滤器都应该应用于屏幕上的datatable,但当通过
downloadHandler()
时,它不会导出过滤后的数据。它过滤掉已过滤的正确行,但不是正确的数据

我正在努力想出一个解决方案,在下载文件时将我想要的“反应式”应用程序应用到表中,以便得到理想的结果,即导出的数据是应用程序中过滤的实际数据

库(闪亮)
图书馆(DT)
图书馆(GG2)
##==加载示例数据=====
汽车=mtcars
#我是否使数据帧具有反应性?
反应性DF=反应性(车辆)
##==用户界面====

ui您没有以正确的方式使用被动对象。在shiny中,这类对象背后的想法是避免重复自己。在这里,您可以创建一个reactive对象
reactiveDF
,并使用该对象生成dataTableOutput和download表

library(shiny)
library(DT)
library(ggplot2)


## === LOAD EXAMPLE DATA =====

cars = mtcars


# do I make the data frame reactive?



## ==== UI ====

ui <- fluidPage(
  titlePanel("Basic DataTable"),
  
  # Create a new Row in the UI for selectInputs
  fluidRow(
    column(4,
           selectInput("cyl",
                       "Cylinders:",
                       c("All",
                         unique(sort(as.character(cars$cyl)))))
    ),
    column(4,
           selectInput("gear",
                       "Gears:",
                       c("All",
                         unique(as.character(cars$gear))))
    ),
    column(4,
           selectInput("carb",
                       "Carburators:",
                       c("All",
                         unique(as.character(cars$carb))))
    )
  ),
  # Create a new row for the table.
  
  DT::dataTableOutput("dt"),
  
  p("Notice that the 'rows_all' attribute grabs the row indices of the data."),
  verbatimTextOutput("filtered_row"),
  
  # Create a new row for the download button
  p("The table responds to the filters perfectly, yet it will not download the data. Test it out."),
  p("Evaluate the csv file and you will see that while it extracts the filtered rows,"),
  p("it is not displaying the actual filtered data in the table!"),
  
  downloadButton(outputId = "download_filtered",
                 label = "Download Filtered Data")
)


## ==== END UI ====

## ==== SERVER ====

server = function(input, output){
  
  
  reactiveDF = reactive({
    cardata <- cars
    # conditionals 
    
    if (input$cyl != "All") {
      cardata <- cardata[cardata$cyl == input$cyl, ]
    }
    if (input$gear != "All") {
      cardata <- cardata[cardata$gear == input$gear, ]
    }
    if (input$carb != "All") {
      cardata <- cardata[cardata$carb == input$carb, ]
    }
    
    # display the filtered data
    cardata
    
    
  })
  
  output$filtered_row <- 
    renderPrint({
      input[["dt_rows_all"]]
    })
  
  # Filter data based on selections
  
  # This controls the 3 filters displayed on top of the datatable
  output$dt <- DT::renderDataTable(datatable({
    reactiveDF()
  }))
  
  
  
  # Download handler for exporting the data to csv -this won't work as intended -----
  
  output$download_filtered <- 
    downloadHandler(
      filename = "filtered_report.csv",
      content = function(file){
        write.csv(reactiveDF(),file)
      }
    )
}

shinyApp(ui,server)


库(闪亮)
图书馆(DT)
图书馆(GG2)
##==加载示例数据=====
汽车=mtcars
#我是否使数据帧具有反应性?
##==用户界面====

你试过我的答案了吗?是的,谢谢-你的答案出来时我正在睡觉。:)你的回答被接受为解决方案。非常感谢约翰!是的,我知道reactiveDF是解决方案的关键,但我在如何将其包装到筛选条件中缺少了联系。你总结得很好。我对代码进行了测试,结果完全正常。