如何下载闪亮应用程序中的图(以jpeg / png格式)

时间:2018-05-10 17:59:41

标签: shiny

我试图找出如何使用downloadButton下载带有光泽的情节。包中的示例演示了downloadButton / downloadHandler以保存.pdf。我将基于此做出一个可重复的例子。

    output$downloadPlot <- downloadHandler(
    filename = function() {
        "plotname .pdf"
    },

    content = function(file) {
        pdf(file = file,
                width = 12,
                height = 12)
        print(buildPlot())
        dev.off()
    }
)       

1 个答案:

答案 0 :(得分:4)

我建议你使用Highcharter包。这样您就不需要创建下载按钮,因为该图表具有可在多个扩展中下载的选项。这里我给你一个直方图的例子,选择以PNG,SVG,JPEG或PDF格式导出。

## Export charts with Highcharter in Shiny

# Load package
library('highcharter')

# UI side
highchartOutput('plot')

# Server side
output$plot <- renderHighchart({

  # Define your data, here I am using Iris dataset as example
  DT <- iris$Sepal.Length 

  # Define export options
  export <- list(
    list(
      text = "PNG",
      onclick = JS("function () {
                   this.exportChart({ type: 'image/png' }); }")
      ),
    list(
      text = "JPEG",
      onclick = JS("function () {
                   this.exportChart({ type: 'image/jpeg' }); }")
      ),
    list(
      text = "SVG",
      onclick = JS("function () {
                   this.exportChart({ type: 'image/svg+xml' }); }")
      ),
    list(
      text = "PDF",
      onclick = JS("function () {
                   this.exportChart({ type: 'application/pdf' }); }")
      )
    )

  # Plot histogram
  hchart(DT,
    type = "area",
    name = colnames(iris[1])
  ) %>%
    hc_exporting(
      enabled = TRUE,
      formAttributes = list(target = "_blank"),
      buttons = list(contextButton = list(
        text = "Export",
        theme = list(fill = "transparent"),
        menuItems = export
      ))
    )
})

输出应该是这样的: enter image description here

希望这有帮助。

Wlademir。