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