【问题标题】:Downloading file from a datatable and pdf graph with 2 download buttons使用 2 个下载按钮从数据表和 pdf 图表下载文件
【发布时间】:2017-11-22 18:27:18
【问题描述】:

我需要从 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 <- ....

   )

【问题讨论】:

    标签: r ggplot2 shiny dt


    【解决方案1】:

    你需要为表格添加一些类似的东西

    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)
      }  
    )
    

    【讨论】:

    • 嗨 Bertil - 再次感谢您的回答 - 它工作正常,我只是用 ggsave 替换导出功能 -
    猜你喜欢
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 2021-12-19
    • 2020-10-10
    • 1970-01-01
    • 2015-01-07
    • 2014-10-13
    • 1970-01-01
    相关资源
    最近更新 更多