I am writing a Shiny application that allows the user to input a CSV file and convert it to a specific XML specifications (OPML, the standard for RSS feeds).
My current issue is uniting the ability to select specific rows and export in the custom format. I can export everything together with the standard downloadButton()
with a custom function I've written (generate_opml()
) that converts into the proper XML format:
output$downloadOPML <- downloadHandler(
filename = function() {
paste('journal-rss-', Sys.Date(), '.opml', sep='')
},
content = function(con) {
opml <- generate_opml(db)
cat(saveXML(opml), file = con)
}
)
However, I want to be able to export only selected rows, which seems easiest DT::datatable()
. This can easily be exported as CSV and few other formats:
output$user_table <- renderDataTable({
datatable(
db,
selection = "none",
filter="top",
rownames = FALSE,
extensions = c("Buttons", "Select"),
options = list(
select = TRUE,
dom = 'Blfrtip',
buttons =
list('copy', 'print', list(
extend = 'collection',
buttons = list(
list(extend = 'csv', filename = "File", title = NULL,
exportOptions = list(modifier = list(selected = TRUE))),
list(extend = 'excel', filename = "File", title = NULL,
exportOptions = list(modifier = list(selected = TRUE)))),
text = 'Download'
))
),
class = "display"
)
So my question is whether it's possible to export a custom file type with DT::datatable()
? Or is there a way to get downloadButton()
to only export selected rows?
CodePudding user response:
The indices of the selected rows are in input$ID_rows_selected
where ID
is the output id of the datatable. So you can do:
output$downloadOPML <- downloadHandler(
filename = function() {
paste('journal-rss-', Sys.Date(), '.opml', sep='')
},
content = function(con) {
dat <- db[input$user_table_rows_selected, ]
opml <- generate_opml(dat)
cat(saveXML(opml), file = con)
}
)
However I'm not sure this works if you use the Select
extension. Maybe with server=FALSE
. Please check.