Download one excel file with multiple sheets as data frames Shiny R

2,152 views
Skip to first unread message

Marieta Baramova

unread,
Jan 21, 2016, 12:08:50 PM1/21/16
to Shiny - Web Framework for R

Hello team,

Hope that you are all well.

I am creating a web application and I have some problem with file downloading. 
When the user click on the button called "downloadTab", I want to download the table outputs into excel file  with multiple sheet. Each sheet is corresponding to a table output.
So,I have 4 data frames (dataTab1,dataTab2,dataTab3,dataTab4) that I want to download as xls file but it doesn't work. Here is my code:

server:

output$downloadTab <- downloadHandler(
      
      filename = function() { paste("tab", " ",Sys.Date(),".xlsx",sep="") },
      content = function(file) {

 #creation of the workbook
        dataxls=createWorkbook(file)
#creation of the sheets
        dataTabs1=createSheet(wb=dataxls,sheetName="Compartiments-simulation_sans_changement") 
        dataTabs2=createSheet(wb=dataxls,sheetName="Esperance-simulation_sans_changement")         
        dataTabs3=createSheet(wb=dataxls,sheetName="Compartiments-simulation_avec_changement") 
        dataTabs4=createSheet(wb=dataxls,sheetName="Esperance-simulation_avec_changement")
 #add the dataframes to the sheets
        addDataFrame(write.xlsx(dataTab1, row.names=F),dataTabs1)
        addDataFrame(write.xlsx(dataTab2, row.names=F),dataTabs2)
        addDataFrame(write.xlsx(dataTab3, row.names=F),dataTabs3)
        addDataFrame(write.xlsx(dataTab4, row.names=F),dataTabs4)
       saveWorkbook(dataxls)
        write.xlsx(dataxls,file,row.names=F)
             
        }
    )

I obtain the following error when I run this code: Unknown format C:\Users\BARAMO~1\AppData\Local\Temp\RtmpMfiJtq\file16541b603a7f.xlsx

Could someone help me, please?

If you have other ideas they are wellcome!

Austin T

unread,
Jan 22, 2016, 3:02:27 AM1/22/16
to Shiny - Web Framework for R
Hi Marieta,

I'm not exactly an expert on this package, but doesn't saveWorkbook() also require an additional argument to save the workbook to? I noticed that you didn't include file one of its arguments.

Sincerely,
Austin

Alan Parker

unread,
Jan 27, 2016, 4:40:32 AM1/27/16
to Shiny - Web Framework for R
Marieta,
               I think you can simplify this code a lot. In any case, you should not use addDataFrame AND write.xlsx in the same line. That's a mistake, see the help pages :-).  I just use "write.xlsx" to write a data frame directly to an Excel file page. No need to use createWorkbook or createSheet or addDataframe if there is only one data frame per page. I do not see what the last writexlsx is for either.
So:
1 Create Excel filename
2 Use write.xlsx to write each data frames to the Excel file.

Maybe I'm missing something, but I don't think you need to go via a workbook object to get what you want.

Amicalement,

Alan
Reply all
Reply to author
Forward
0 new messages