从R Shiny下载所有高图表输出

时间:2018-12-26 05:05:16

标签: r highcharts

我在Shiny页面上有一组图表和几个表。创建下载按钮以允许用户将所有文件下载到一个文件(即pdf)的最佳方法是什么。我尝试过grid.arrange,但不确定如何将highchart对象转换为grobs对象。

输出示例如下:

library("shiny")
library("highcharter")

data(citytemp)

ui <- fluidPage(
  fluidRow(

        column(width = 8,
           highchartOutput("hcontainer1",height = "500px")),
        column(width = 8,
                  highchartOutput("hcontainer2",height = "500px")),
        column(width = 8,
               highchartOutput("hcontainer3",height = "500px")),
        column(width = 12,dataTableOutput("table"))
  )
)

server = function(input, output) {

  output$hcontainer1 <- renderHighchart({
    hc <- highcharts_demo() %>%
      hc_rm_series("Berlin") %>% 
      hc_chart(type = "line")
    hc
  })

  output$hcontainer2 <- renderHighchart({
    hc <- highcharts_demo() %>%
      hc_rm_series("Berlin") %>% 
      hc_chart(type = "bar")
    hc
  })

  output$hcontainer3 <- renderHighchart({
    hc <- highcharts_demo() %>%
      hc_rm_series("Berlin") %>% 
      hc_chart(type = "column")
    hc
  })

  output$table <- renderDataTable({
    dt <- data.frame(iris[1:10,])
    dt
  })

}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

只需尝试在每列上放置一个下载按钮。我现在无法安装highcharter,所以我没有完全测试代码。让我知道您是否遇到问题-

column(width = 8,
           highchartOutput("hcontainer1",height = "500px"), downloadButton("downloadplot1", label = "Download"))

编写一个单独的函数以生成图-

gen_plot <- function(series_name, chart_type){
    hc <- highcharts_demo() %>%
      hc_rm_series(series_name) %>% 
      hc_chart(type = chart_type)
    hc
}

现在位于server-

output$downloadplot1 <- downloadHandler(filename ="1.png",
                                         content = function(file) {
                                           png(file, width=800, height=800)
                                           gen_plot("Berlin", "line")
                                           dev.off()
                                         },
                                         contentType = "image/png")