简体   繁体   English

从Shiny中的renderDataTable()下载过滤后的数据

[英]Download filtered data from renderDataTable() in Shiny

I am trying to allow users to download filtered data from renderDataTable() in Shiny. 我试图允许用户从Shiny中的renderDataTable()下载过滤数据。 In the MWE below, for instance, the user should be able to go to the "See Whole Data Table" tab and, say, select only values of "Blue" for Variable2. 例如,在下面的MWE中,用户应该能够转到“查看整个数据表”选项卡,例如,只为Variable2选择“蓝色”值。 Then, they should be able to click the "Download the filtered data" button and see that filtered data. 然后,他们应该能够单击“下载过滤数据”按钮并查看过滤后的数据。

However, as the code is below, when the user clicks on the "Download the filtered data" button, they download the entire (unfiltered) data. 但是,由于代码如下,当用户点击“下载过滤数据”按钮时,他们会下载整个(未过滤的)数据。 I have seen other examples (such as R - Download Filtered Datatable ) and tried to incorporate them into my code, but remain stuck. 我已经看到了其他示例(例如R - Download Filtered Datatable )并尝试将它们合并到我的代码中,但仍然卡住了。 Any advice would be so helpful. 任何建议都会很有帮助。 Thank you. 谢谢。

# Load packages
library(shiny)
library(tidyr)
library(dplyr)

# Load data
data_table <- data.frame(Variable1 = sample(LETTERS, 1000, replace=TRUE), Variable2 = sample(c("Orange","Green","Blue","Pink"), 1000, replace=TRUE), Variable3 = sample(c("Square","Triangle","Circle"), 10000, replace=TRUE))

# Define UI
ui <- fluidPage(
  tabsetPanel(type = "tabs", id="tabs",
    tabPanel("Column Summary", value=2,
       sidebarPanel(uiOutput("sidebar_summary")),
       verbatimTextOutput("summary")),
    tabPanel("See Whole Data Table", value=5,
       downloadButton('downLoadFilter',"Download the filtered data"),
       verbatimTextOutput("Raw"),
       DT::dataTableOutput('ex1'))
  )
)

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

  output$sidebar_summary <- renderUI({
    if (input$tabs == 2){
    print("This is a tab with information.")
    }
  })

  thedata <- reactive(data_table)

  output$Raw <- renderPrint({
    if(input$tabs == 5){
      output$ex1 <- DT::renderDataTable(DT::datatable(thedata(), filter = 'top',escape = FALSE, options = list(pageLength = 10, scrollX='500px',autoWidth = TRUE)))
    }
  })

  output$downLoadFilter <- downloadHandler(
    filename = function() {
      paste('Filtered data-', Sys.Date(), '.csv', sep = '')
    },
    content = function(file){
      write.csv(thedata(),file)
    }
  )
}

# Create Shiny object
shinyApp(ui = ui, server = server)

Replicating the example from the link you provided it seems to work. 从您提供的链接复制示例似乎可行。

You just need to replace 你只需要更换

output$downLoadFilter <- downloadHandler(
    filename = function() {
      paste('Filtered data-', Sys.Date(), '.csv', sep = '')
    },
    content = function(file){
      write.csv(thedata(),file)
    }
  )

in your server with 在你的服务器上

 output$downLoadFilter <- downloadHandler(
    filename = function() {
      paste('Filtered data-', Sys.Date(), '.csv', sep = '')
    },
    content = function(file){
      write.csv(thedata()[input[["ex1_rows_all"]], ],file)
    }
  )

That does the trick. 这就是诀窍。 Hope it helps! 希望能帮助到你!

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

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