扩展程序Buttons
非常适合来自library(DT)
的闪亮应用程序。但是它导出数据而不进行格式化。有没有办法以格式(例如百分比或货币)导出数据? Similar question尚未解决。
可重复的代码
library(DT)
data.frame(a = c(1,2),
b = c(2,3)) %>%
datatable(extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')) )%>%
formatPercentage('a') %>%
formatCurrency('b')
答案 0 :(得分:1)
您可以使用TableExport库而不是使用Buttons扩展。
library(shiny)
library(DT)
library(shinyjs)
js_export <-
"
var $table = $('#DTtable').find('table');
var instance = $table.tableExport({
formats: ['xlsx'],
exportButtons: false,
filename: 'myTable',
sheetname: 'Sheet1'
});
var exportData0 = instance.getExportData();
var exportData = exportData0[Object.keys(exportData0)[0]]['xlsx'];
instance.export2file(exportData.data, exportData.mimeType, exportData.filename,
exportData.fileExtension, exportData.merges,
exportData.RTL, exportData.sheetname);
"
ui <- fluidPage(
useShinyjs(),
tags$head(
# put these files in the www subfolder
tags$script(src = "xlsx.core.min.js"),
tags$script(src = "FileSaver.min.js"),
tags$script(src = "tableexport.min.js")
),
DTOutput("DTtable"),
actionButton("export", "Export table")
)
server <- function(input, output, session){
output$DTtable <- renderDT({
data.frame(
a = c(1,2),
b = c(2,3)
) %>%
datatable() %>%
formatPercentage('a') %>%
formatCurrency('b')
})
observeEvent(input$export, {
runjs(js_export)
})
}
shinyApp(ui, server)