Download Button for Excel file

Hi!

How can I make a function button to download a DataFrame in Excel format (.xls)?

I have this DF:

UI Code:

 DT::dataTableOutput("contents2")


 downloadButton("downloadData", "Baixar Planilha de Dados"),

Server:

output$contents2 = DT::renderDataTable({
    tabela_saida
  })

... code for download the dataTable that i don't know how to do

I tried to follow the example from "https://shiny.rstudio.com/gallery/file-download.html' but it did not work.

Can someone help?

Thx!

Since you're already using a DataTable, try looking into adding the Buttons extension which allows you to download the data in the table. See this https://rstudio.github.io/DT/extensions.html

3 Likes

If a CSV file is "good enough" Excel for you - you can consider this in your server side code:

 # pres download data -> initiate save csv
   output$downloadData <- downloadHandler(
     
     filename = "downloaded_data.csv",
     content = function(file) write.csv2(times, file, row.names = F),  # replace "times" with your data.frame to export'
     contentType = "text/csv"
     
   )

A side note: download buttons are known to behave strangely in Shiny preview (don't ask me why) so be sure to try troubleshoot it live.

Also consider between write.csv and write.csv2 depending on your formatting customs (coma for separator or decimal places and so on...)

5 Likes

Really thanks, both of you, @jlacko and @jdb

It works!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.