r - 在 Shiny 中下载过滤后的 tableOutput
问题描述
我有以下数据:
> data
products id
1 P1 280386
2 P1 285184
3 P2 293154
4 P1 294245
我已经构建了一个简单的闪亮代码。我首先过滤表,然后我想下载过滤后的表。我写以下
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)
但是,此代码仅下载完整表,而不是过滤后的表。我搜索了一些问题,但没有一个具体解释这个案例。提前致谢
解决方案
尝试这个
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)
推荐阅读
- mysql - mysql有条件地在where子句中应用乘法因子
- python - colouredlogs python 格式错误,带有多个模块和文件 [Python 3.9/Windows 10]
- c++ - 将节点添加到链表的末尾但无法访问最后一个节点
- questdb - 如何在 GCP 上部署 QuestDB?
- scala - 根据文件中的数据创建多个列表
- python - 单个数据点的 SHAP,而不是整个数据集的平均预测
- c++ - 我应该在使用 std::transform 之前检查向量是否为空
- javascript - Owl Carousel 使用 autoPlayTimeout 限制循环次数不会停止循环
- ruby-on-rails - 在 docker swarm 中沿侧轨 puma 容器托管 sidekiq 容器
- file - 使用 curl 从管道发布文件