我需要从2个SQL查询中导出图形和xls 我的图表通过单击行与数据表链接 我放置了2个下载按钮,但我不知道如何用按钮触发导出(可能还有另一个反应函数?)
感谢您的帮助
这是我的UI.R:
mainPanel(
DT::dataTableOutput("table"), #My Table
plotOutput("plot")) # My graph
downloadButton("plot_export", "PDF"),
# Button
downloadButton("downloadData", "XLS")
))
这里是server.R:
cpk_total <- reactive({
data_testeur <- odbcConnect(input$base, uid="uid")
SQL query to feed my dataTable
Close connexion data_testeur
return result created from the SQL query
})
output$Table <- DT::renderDataTable({
DT::datatable(cpk_total(),...) # Formating table
})
output$plot <- renderPlot({
dta <- cpk_total()
data_testeur <- odbcConnect(input$base, uid="uid")
another SQL query to trace the graph for 1 item selected
#This SQL query use a variable from the created cpk_total table
Close connexion data_testeur
graph <- ....
)
答案 0 :(得分:0)
你需要为表添加这样的东西
output$downloadData <- downloadHandler (
filename = function() {
#some function to generate your file name
},
content = function(file) {
dta <- cpk_total()
write.csv2(dta, file, row.names = FALSE, fileEncoding = "UTF-8", quote = FALSE, na = "")
}
)
对于图表,我会将代码从renderPlot
拉出到单独的reactive
,就像您使用cpk_total()
并添加类似的内容以下载情节
output$downloadData <- downloadHandler (
filename = function() {
#some function to generate your file name
},
content = function(file) {
p <- reactive_plot()
export(p, file = file)
}
)