簡體   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