Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从中的renderDataTable()下载筛选数据_R_Shiny - Fatal编程技术网

从中的renderDataTable()下载筛选数据

从中的renderDataTable()下载筛选数据,r,shiny,R,Shiny,我试图允许用户从renderDataTable中下载过滤后的数据。例如,在下面的MWE中,用户应该能够转到“查看整个数据表”选项卡,并(比如)仅为Variable2选择蓝色值。然后,他们应该能够单击下载过滤数据按钮并查看过滤数据 但是,正如下面的代码所示,当用户单击Download the filtered data(下载过滤数据)按钮时,他们会下载整个未过滤数据。我已经看到了其他的例子,例如,并试图将它们合并到我的代码中,但仍然无法解决。任何建议都会很有帮助。多谢各位 # Load packa

我试图允许用户从renderDataTable中下载过滤后的数据。例如,在下面的MWE中,用户应该能够转到“查看整个数据表”选项卡,并(比如)仅为Variable2选择蓝色值。然后,他们应该能够单击下载过滤数据按钮并查看过滤数据

但是,正如下面的代码所示,当用户单击Download the filtered data(下载过滤数据)按钮时,他们会下载整个未过滤数据。我已经看到了其他的例子,例如,并试图将它们合并到我的代码中,但仍然无法解决。任何建议都会很有帮助。多谢各位

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

从您提供的示例中复制该示例似乎是可行的

你只需要换一个

output$downLoadFilter <- downloadHandler(
    filename = function() {
      paste('Filtered data-', Sys.Date(), '.csv', sep = '')
    },
    content = function(file){
      write.csv(thedata(),file)
    }
  )
在服务器中使用

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

这就是诀窍。希望有帮助

复制您提供的示例似乎有效

你只需要换一个

output$downLoadFilter <- downloadHandler(
    filename = function() {
      paste('Filtered data-', Sys.Date(), '.csv', sep = '')
    },
    content = function(file){
      write.csv(thedata(),file)
    }
  )
在服务器中使用

 output$downLoadFilter <- downloadHandler(
    filename = function() {
      paste('Filtered data-', Sys.Date(), '.csv', sep = '')
    },
    content = function(file){
      write.csv(thedata()[input[["ex1_rows_all"]], ],file)
    }
  )
这就是诀窍。希望有帮助