首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Shiny中下载过滤的tableOutput

在Shiny中下载过滤的tableOutput
EN

Stack Overflow用户
提问于 2020-11-12 18:06:55
回答 1查看 29关注 0票数 0

我有以下数据:

代码语言:javascript
复制
> data
        products         id
1             P1     280386
2             P1     285184
3             P2     293154
4             P1     294245

我已经构建了一个简单的闪亮代码。我首先对表进行筛选,然后我想下载经过筛选的表。我写了以下内容

代码语言:javascript
复制
library(shiny)
library(shinyWidgets)
library(tidyverse)
library(DT)

data <- read.csv("Desktop/data.csv")
products <- unique(data$products)


ui <- fluidPage(
  fluidRow(
    column(4, 
           selectInput("product", "Product", products,
                       multiple = TRUE), 
           downloadButton("download", "Download")),
    column(8, 
           tableOutput("errorTable")
    )
  )
)

server <- function(input, output, session) {
  
  output$errorTable <- renderTable({
    subset(data, products == input$product) 
  }
  )
  
  
  output$download <- downloadHandler(
    filename = function() {
      paste("data-",Sys.Date(), ".csv", sep = "")
    },
    content = function(file) {
      write.csv(data, file)
    }
  )
  
}

shinyApp(ui, server)

但是,此代码仅下载整个表,而不下载过滤后的表。我搜索了一些问题,但没有人具体解释这个案例。提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-12 18:17:10

尝尝这个

代码语言:javascript
复制
library(shiny)
library(shinyWidgets)
library(tidyverse)
library(DT)

data <- read.csv("Desktop/data.csv")
products <- unique(data$products)


ui <- fluidPage(
  fluidRow(
    column(4, 
           selectInput("product", "Product", products,
                       multiple = TRUE), 
           downloadButton("download", "Download")),
    column(8, 
           tableOutput("errorTable")
    )
  )
)

server <- function(input, output, session) {
  
  
  #you need to create a reactive object with a NULL starting value
  listofitems <- reactiveValues(data = NULL )
  
  #observe the changes in input$product and update the reactive object 
  observeEvent( input$product, {
    print("Hello: observeEvent for input$product is triggered")
    #debug using browser()
    listofitems$data <-  subset(data, products == input$product) 

    showNotification("Products updated",
                     type =  "message",
                     duration = 4,
                     closeButton = TRUE)
  }, ignoreInit = T,ignoreNULL = TRUE)
  

    
    output$errorTable <- renderTable({
      listofitems$data
    }
    )
  
  
  output$download <- downloadHandler(
    filename = function() {
      paste("data-",Sys.Date(), ".csv", sep = "")
    },
    content = function(file) {
      write.csv(listofitems$data, file)
    }
  )
  
}

shinyApp(ui, server)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64801841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档