简体   繁体   English

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

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

Good afternoon, thank you in advance for taking the time to read my question.下午好,提前感谢您抽出时间阅读我的问题。 In my Shiny app, I am trying to create a list of the reactive objects to write to a single excel workbook for the user to download.在我的 Shiny 应用程序中,我正在尝试创建一个反应对象列表以写入单个 excel 工作簿供用户下载。 I was able to use parts of responses from other posts to replicate my issue and I get pretty close to a resolution.我能够使用其他帖子的部分回复来复制我的问题,并且我非常接近解决方案。 However, while the example below uses a list of dataframes, such as mtcars, iris, etc, I am trying to use reactive datasets, such as datasetInput1(), datasetInput2(), etc.但是,虽然下面的示例使用了数据帧列表,例如 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)
                }
            }
        )

When I try to use these reactive objects in the example below, I am able to successfully download the data when there is just one dataset in the list.当我尝试在下面的示例中使用这些反应对象时,当列表中只有一个数据集时,我能够成功下载数据。 For example, the below works, but once I start adding more to the list listOtherFiles such as listOtherFiles <- list(datasetInput2(), datasetInput3()), I get an error.例如,以下内容有效,但是一旦我开始向列表 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() is a reactive value not defined within the server logic. datasetInput1() 是未在服务器逻辑中定义的反应值。 This needs to be assigned a value first or a function created to update values.这需要首先分配一个值,或者创建一个 function 以更新值。

The following are some useful articles to understand shiny's reactive elements:以下是一些有用的文章,可帮助您了解 Shiny 的反应性元素:

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

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

I'm not sure I can reproduce the problem.我不确定我能否重现该问题。 Here is my example below.下面是我的例子。 This seems to work and uses two reactive expressions.这似乎有效并使用了两个reactive表达。 Does it work for you?对你起作用吗?

If not, please edit your question and describe further.如果没有,请编辑您的问题并进一步描述。 Perhaps include example data and ui with inputs to reproduce.也许包括示例数据和带有输入的ui以进行重现。 What was your error?你的错误是什么?

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