【发布时间】:2019-03-13 10:41:41
【问题描述】:
我制作了一个闪亮的应用程序,我在其中使用一些值过滤数据集,然后我希望能够下载该过滤后的数据集。但是,我很难理解如何将过滤后的数据集传递给 csv 下载器。这是一个非常大的数据集,因此无法使用 renderDataTable 中可用的按钮(我认为?)有人知道我该怎么做吗?
示例应用:
### data ###
egDf <- data.frame(col1 = sample(letters,10000,replace=T), col2 = sample(letters,10000, replace=T))
### modules ###
chooseCol1UI <- function(id){
ns <- NS(id)
uiOutput(ns('chooserCol1'))
}
chooseCol1 <- function(input, output, session, data){
output$chooserCol1 <- renderUI({
ns <- session$ns
pickerInput(inputId = ns('chosenCol1'),
label = 'Col1',
choices = paste(sort(unique(egDf$col1))),
options = list(`actions-box` = TRUE),
multiple = TRUE)
})
return(reactive(input$chosenCol1))
}
csvDownloadUI <- function(id, label = "Download CSV") {
ns <- NS(id)
downloadButton(ns("downloadData"), label)
}
csvDownload <- function(input, output, session, data) {
output$downloadData <- downloadHandler(
filename = function() {
paste(names(data), Sys.Date(), '.csv', sep='')
},
content = function(file) {
write.csv(data, file, row.names = FALSE)
}
)
}
displayTableUI <- function(id){
ns <- NS(id)
DT::dataTableOutput(ns('displayer'))
}
displayTable <- function(input, output, session, data, col1Input){
output$displayer <- DT::renderDataTable(egDf %>% filter(col1 %in% col1Input()))
}
### server ###
server <- function(input,output){
chosenCol1 <- callModule(chooseCol1,
id = 'appChooseCol1', data = egDf)
callModule(module = displayTable, id = "appDisplayTable",
col1Input = chosenCol1)
}
### ui ###
ui <- fluidPage(
sidebarPanel(
chooseCol1UI("appChooseCol1")),
mainPanel(displayTableUI("appDisplayTable")))
### app ###
shinyApp(ui = ui, server = server)
【问题讨论】:
-
基本上,您是在问如何使用闪亮的下载按钮。将数据保存在变量中并将其传递给
downloadHandler()。查看示例 here,从 R 运行示例应用程序:runExample("10_download") -
我认为问题更多是关于如何从 displayTable 中获取过滤后的数据 - 对吧?
-
@ill 正是,这就是我想要做的
标签: r shiny shiny-reactivity shinymodules