繁体   English   中英

使用多个反应数据集写入到 downloadHandler shiny 下的 excel 工作簿

[英]Use multiple reactive datasets to write to an excel workbook under downloadHandler shiny

下午好,提前感谢您抽出时间阅读我的问题。 在我的 Shiny 应用程序中,我正在尝试创建一个反应对象列表以写入单个 excel 工作簿供用户下载。 我能够使用其他帖子的部分回复来复制我的问题,并且我非常接近解决方案。 但是,虽然下面的示例使用了数据帧列表,例如 mtcars、iris 等,但我正在尝试使用响应式数据集,例如 datasetInput1()、datasetInput2() 等。

shinyApp(
    ui = fluidPage(
        downloadButton("downloadExcelSheet", "Download Excel Workbook with Multiple Sheets")
    ),
    server = function(input, output) { 

        #### Write an Excel workbook with one sheet per dataframe ####
        output$downloadExcelSheet <- downloadHandler(
            filename = function() {
                "excelWorkbook.xlsx"
            },
            content = function(file) {
                # write workbook and first sheet
                write.xlsx(mtcars, file, sheetName = "mtcars", append = FALSE)

                # add other sheets for each dataframe
                listOtherFiles <- list(iris = iris, 
                                       airquality = airquality, 
                                       sleep = sleep)
                for(i in 1:length(listOtherFiles)) {
                    write.xlsx(listOtherFiles[i], file, 
                               sheetName = names(listOtherFiles)[i], append = TRUE)
                }
            }
        )

当我尝试在下面的示例中使用这些反应对象时,当列表中只有一个数据集时,我能够成功下载数据。 例如,以下内容有效,但是一旦我开始向列表 listOtherFiles 添加更多内容,例如 listOtherFiles <- list(datasetInput2(), datasetInput3()),就会出现错误。

shinyApp(
    ui = fluidPage(
        downloadButton("downloadExcelSheet", "Download Excel Workbook with Multiple Sheets")
    ),
    server = function(input, output) { 

datasetInput1 <- reactive({

    data %>% 
      filter(sub_date == input$date, app_type == input$type)
})


datasetInput2 <- reactive({

    data2 %>% 
      filter(sub_date == input$date, app_type == input$type)
})

        output$downloadExcelSheet <- downloadHandler(
            filename = function() {
                "datasetOutput.xlsx"
            },
            content = function(file) {
                # write workbook and first sheet
                write.xlsx(datasetInput1(), file, sheetName = "dataset1", append = FALSE)

                # add other sheets for each dataframe
                listOtherFiles <- list(datasetInput2())

                for(i in 1:length(listOtherFiles)) {
                    write.xlsx(listOtherFiles[i], file, 
                               sheetName = names(listOtherFiles)[i], append = TRUE)
                }
            }
        )

datasetInput1() 是未在服务器逻辑中定义的反应值。 这需要首先分配一个值,或者创建一个 function 以更新值。

以下是一些有用的文章,可帮助您了解 Shiny 的反应性元素:

https://shiny.rstudio.com/articles/understanding-reactivity.html

https://shiny.rstudio.com/articles/reactivity-overview.html

我不确定我能否重现该问题。 下面是我的例子。 这似乎有效并使用了两个reactive表达。 对你起作用吗?

如果没有,请编辑您的问题并进一步描述。 也许包括示例数据和带有输入的ui以进行重现。 你的错误是什么?

library(xlsx)
library(shiny)
library(tidyverse)

shinyApp(
  ui = fluidPage(
    downloadButton("downloadExcelSheet", "Download Excel Workbook with Multiple Sheets")
  ),
  server = function(input, output) { 

    datasetInput1 <- reactive({
      iris %>% 
        filter(Species == "setosa")
    })

    datasetInput2 <- reactive({
      iris %>% 
        filter(Species == "versicolor")
    })

    #### Write an Excel workbook with one sheet per dataframe ####
    output$downloadExcelSheet <- downloadHandler(
      filename = function() {
        "excelWorkbook.xlsx"
      },
      content = function(file) {
        # write workbook and first sheet
        write.xlsx(mtcars, file, sheetName = "mtcars", append = FALSE)

        # add other sheets for each dataframe
        listOtherFiles <- list(setosa = datasetInput1(), versicolor = datasetInput2())
        for(i in 1:length(listOtherFiles)) {
          write.xlsx(listOtherFiles[[i]], file, 
                     sheetName = names(listOtherFiles)[i], append = TRUE)
        }
      }
    )
  }
)

暂无
暂无

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

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