r - Download multiple dataframes in multiple sheets of the same excel file in a shiny app
问题描述
I wonderif there is a way to download 2 dataframes in the same excel file but in different sheet via shiny app.
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)
解决方案
try changing your server code to this. Also, remember to open the app in your browser and not just the rstudio viewer (assuming your are using rstudio). Hope this helps!
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
}
)
})
推荐阅读
- php - 当这样的结构时,如何使用mysql数据库制作下拉菜单?
- pandas - 熊猫在画布中绘制多个df
- angular - Angular Promise - debounceTime 行为
- html - 在css中调整图像大小以自动调整div
- python - pandas groupby 功能无法使用
- sql-server - SQL Server 中的索引列
- c# - IAsyncDisposable, IDisposable 在基类中处理内部 IAsyncDisposable 成员
- python - Python 矩阵/向量运算
- networking - 如何用openflow修改ip数据包中的特殊位?
- arrays - 无法找到一个项目是否存在于项目数组中并在 Perl 中返回必要的消息