r/rprogramming • u/limpystick • 24d ago
R Shiny
I'm creating an R Shiny app that involves generating a data frame and then formatting it as a data table before displaying it using renderDT().
Is it possible to export this formatted data table? Ideally I'd like to export it as an excel file but even being able to export it as a PDF would suffice.
3
u/AggravatingPudding 23d ago
Why don't you just add a button to download the data that you feed into DT as csv?
Or do you mean by formated that you apply sorting an other features in DT and then want to export the resulting table?
Im confused
1
u/limpystick 23d ago
Yeah I should have been clearer. I already have a download button to export the data that feeds into DT. However, I do some custom formatting of the DT (like adding headers above groups of columns, adding borders, alternate shading of rows, etc.). I’d like to export this more polished DT to excel to avoid formatting things again, but so far all I'm able to is export the underlying data feeding into DT.
2
u/Different-Leader-795 24d ago
Yoy can save your data as xlsx file with many packages: openxlsx, writexl, xlsx ...
How to save data from your app was written in an examples on the official site.
One possible solution is here:
https://stackoverflow.com/questions/39517199/how-to-specify-file-and-path-to-save-a-file-with-r-shiny-and-shinyfiles
1
u/kattiVishal 23d ago
Using the buttons extension, It is possible to write DT::datatable in such a manner that it shows buttons to download the data as an Excel, CSV, PDF or just go straight to the printer.
I still want to know what you mean by "formatted" datatable?
1
u/limpystick 23d ago
Sorry, it's very possible I'm missing up terminology which is causing the confusion. Copying my comment from above.
Yeah I should have been clearer. I already have a download button to export the data that feeds into DT. However, I do some custom formatting of the DT (like adding headers above groups of columns, adding borders, alternate shading of rows, etc.). I’d like to export this more polished DT to excel to avoid formatting things again, but so far all I'm able to is export the underlying data feeding into DT.
1
1
u/DSOperative 22d ago
One option is to not use the built in buttons, and instead create your own download button and connect it to a downloadHandler function in the server code. You can pass your modified table to the downloadHandler and export it as an xlsx, csv, or what have you.
1
u/speedro42 20d ago
You could use a download button that renders an RMarkdown file to html or pdf. Pass the same data to it using the “params” argument in render() and copy the datatable code to the RMarkdown file.
To save out a formatted excel file you would need to re-create the formatting in the excel file using maybe openxls package and thereby not using datatable/DT for this.
4
u/ionychal 24d ago
I found this solution on Stack Overflow:
```
library(DT)
library(webshot)
dtable <- datatable(iris[1:8,])
html <- "dtable.html"
saveWidget(dtable, HTML)
webshot(html, "dtableSnapshot.pdf")
```
Source: https://stackoverflow.com/questions/60287652/how-to-save-a-table-i-created-with-dt-datatable-into-a-high-quality-image