在闪亮的应用程序中下载同一 Excel 文件的多个工作表中的多个数据帧

fir*_*o23 1 excel r shiny

我想知道是否有一种方法可以通过闪亮的应用程序在同一个 Excel 文件中但在不同的工作表中下载 2 个数据帧。

library(shiny)
library(xlsx)
ui <- shinyUI(fluidPage(

  titlePanel("Testing File upload"),

  sidebarLayout(
    sidebarPanel(
      downloadButton("dl","Export in Excel")

    ),

    mainPanel(
    )
  )
))

server <- shinyServer(function(input, output) {

  output$dl <- downloadHandler(

    filename = function() {
      paste0("df_dmodel", "_Table", ".xls")
    },
    content = function(file){
      tbl<-iris
      tbl2<-mtcars
      write.xlsx(tbl,tbl2 file, 
                 sheetName = "Sheet1", row.names = FALSE)

    }


  ) 

})

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

And*_*rew 5

尝试将您的服务器代码更改为此。另外,请记住在浏览器中打开应用程序,而不仅仅是 rstudio 查看器(假设您使用的是 rstudio)。希望这可以帮助!

server <- shinyServer(function(input, output) {

    output$dl <- downloadHandler(

        filename = function() {
            paste0("df_dmodel", "_Table", ".xlsx")
        },
        content = function(file){
            tbl<-iris
            tbl2<-mtcars
            sheets <- mget(ls(pattern = "tbl")) # getting all objects in your environment with tbl in the name
            names(sheets) <- paste0("sheet", seq_len(length(sheets))) # changing the names in your list
            writexl::write_xlsx(sheets, path = file) # saving the file
        }
    ) 
})
Run Code Online (Sandbox Code Playgroud)