r - 在 Shiny 的应用程序中下载多张同一个 excel 文件中的多个数据框

标签 r excel shiny

我想知道是否有一种方法可以通过 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)

最佳答案

尝试将您的服务器代码更改为此。另外,请记住在浏览器中打开应用程序,而不仅仅是 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
        }
    ) 
})

关于r - 在 Shiny 的应用程序中下载多张同一个 excel 文件中的多个数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61921067/

相关文章:

javascript - 如何将复选框输入传递给 shinyServer() 中的函数

javascript - Shiny 应用程序是否有 “Save Page As PDF”?

r - 如何获得情节的范围(ylim)?

python - 政治模拟优化选举人票……20万+种可能性,如何跑通?

R:将字符变量编码为数字

vba - '编译错误: User-defined type not defined'

Excel 2003 VBA 代码在单步执行时有效,但在完全运行时无效

r - 基于不同 react 值的 Shiny react 值

r - 绘制也将包含行为的事件预算图

r - 使用 R 的不同绘图系统在 0 处绘制垂直线和水平线