如何使用 R Shiny downloadHandler 下载 ggplotly 图? [英] How to download a ggplotly plot with R Shiny downloadHandler?

查看:155
本文介绍了如何使用 R Shiny downloadHandler 下载 ggplotly 图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 R 中制作一个 Shiny 应用程序.我使用 plotly 使我的 ggplots 具有交互性,因此我在应用程序中有很多 ggplotly 绘图.我希望能够通过界面上的按钮下载每一个.

我的下载按钮适用于普通的 ggplot 对象,但不适用于 ggplotly 对象.一个简单的可重现示例是:

图书馆(闪亮)图书馆(ggplot2)library(processx) # for orca()图书馆(情节)ui <-流体页面(mainPanel(plotlyOutput("plot1"), downloadButton('download1', 'Download Graph')))服务器 <- 功能(输入,输出){make_plot1 <- 函数(){p1 = ggplot(cars, aes(x = speed, y = dist)) + geom_point()返回(ggplotly(p1))}输出$plot1 <- renderPlotly({ make_plot1() })输出$download1 <- downloadHandler(filename = function() {'plot1.png'},内容 = 功能(文件){# 尝试 1png(文件)打印(make_plot1())# 尝试 2#plotly_IMAGE(make_plot1(), format = "png", out_file = file)# 尝试 3#orca(make_plot1(), 文件)#尝试 4#export(make_plot1(), file = file)dev.off()})}闪亮应用程序(用户界面,服务器)

我尝试过的一些事情在该代码中被注释掉了.

Try 1 基于

仪表板取自 https://plot.ly/r/dashboard/.>

来自 plotly 支持论坛 (https://community.plot.ly/t/remove-options-from-the-hover-toolbar/130/3),您可以使用 config() 删除其他组件.

make_plot1 <- function() {p1 = ggplot(cars, aes(x = speed, y = dist)) + geom_point()p1 = ggplotly(p1) %>%配置(modeBarButtonsToRemove = 列表("zoom2d","pan2d","zoomIn2d","zoomOut2d","autoScale2d","resetScale2d","hoverClosestCartesian","hoverCompareCartesian","sendDataToCloud","切换悬停","重置视图","toggleSpikelines",重置ViewMapbox"),displaylogo = FALSE)返回(p1)}

您还可以使用 CSS 移动模式栏,使其不覆盖绘图.

.modebar {顶部:-30px !重要;}

I'm making a Shiny app in R. I use plotly to make my ggplots interactive, thus I have lots of ggplotly plots in the app. I would like to be able to download each one through a button on the interface.

My download button works for normal ggplot objects but not for ggplotly objects. A simple reproducible example would be:

library(shiny)
library(ggplot2)
library(processx) # for orca()
library(plotly)

ui <- fluidPage(
  mainPanel(plotlyOutput("plot1"), downloadButton('download1', 'Download Graph'))
  )

server <- function(input,output){
  make_plot1 <- function(){
    p1 = ggplot(cars, aes(x = speed, y = dist)) + geom_point()
    return(ggplotly(p1))}

  output$plot1 <- renderPlotly({ make_plot1() }) 

  output$download1 <- downloadHandler(
    filename = function() {'plot1.png'},
    content = function(file) {
      # try 1
      png(file)
      print(make_plot1())

      # try 2
      #plotly_IMAGE(make_plot1(), format = "png", out_file = file)

      # try 3
      #orca(make_plot1(), file)

      #try 4
      #export(make_plot1(), file = file)

      dev.off()
      })
  }

shinyApp(ui, server)

Some things I've tried are commented out in that code.

Try 1 is based on how I would normally handle plot objects in a shiny app

Try 2 is based on this question and this post

Try 3 is based on some plotly documentation

Try 4 is based on this question

All of these attempts either download a blank .png (try 1) or simply fail to download anything (tries 2-4). I suspect I'm not quite using the download Handler correctly. Anybody have suggestions for getting this working?

EDIT: For this case I want .png files, but there are some good answers on this thread for downloading interactive .html files.

解决方案

Do you need to accomplish this with a download button for some reason? If not, plotly has its own button in the modebar that downloads to a PNG.

Dashboard taken from https://plot.ly/r/dashboard/.

From the plotly support forum (https://community.plot.ly/t/remove-options-from-the-hover-toolbar/130/3), you can use config() to remove the other components.

make_plot1 <- function() {
  p1 = ggplot(cars, aes(x = speed, y = dist)) + geom_point()
  p1 = ggplotly(p1) %>%
    config(
      modeBarButtonsToRemove = list(
        "zoom2d",
        "pan2d",
        "zoomIn2d",
        "zoomOut2d",
        "autoScale2d",
        "resetScale2d",
        "hoverClosestCartesian",
        "hoverCompareCartesian",
        "sendDataToCloud",
        "toggleHover",
        "resetViews",
        "toggleSpikelines",
        "resetViewMapbox"
      ),
      displaylogo = FALSE
    )
  return(p1)
}

You can also move the modebar so that it doesn't cover the plot, using CSS.

.modebar {
    top: -30px !important;
}

这篇关于如何使用 R Shiny downloadHandler 下载 ggplotly 图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆