【问题标题】:How to Export ggplotly from R shiny app as html file如何将 ggplotly 从 R 闪亮的应用程序导出为 html 文件
【发布时间】:2019-09-08 02:53:19
【问题描述】:

我的闪亮应用中有一个名为 p2 的 ggplotly 对象。我希望它能够在用户按下应用程序 UI 中的下载按钮的情况下工作,它会将 ggplotly 绘图下载为 html 文件。但该功能不起作用:

output$Export_Graph <- downloadHandler(

      filename = function() {
        file <- "Graph"

      },

      content = function(file) {
        write(p2, file)
      }

有什么想法吗?

【问题讨论】:

  • 在那篇文章中,他们将其导出为 png 文件,但我想将其导出为 html 文件。
  • 什么样的 HTML 文件?通常当你这样做时,你会有一个 .rmd 文件,其中包含被编织和下载的情节,但我在这里看不到。您的问题是关于从闪亮中保存 html 报告的更普遍的问题吗?

标签: r ggplotly


【解决方案1】:

可以将交互式绘图图导出为“html 小部件”。 一个最小的示例代码如下:

library(shiny)
library(plotly)
library(ggplot2)
library(htmlwidgets) # to use saveWidget function

ui <- fluidPage(

    titlePanel("Plotly html widget download example"),

    sidebarLayout(
        sidebarPanel(
            # add download button
            downloadButton("download_plotly_widget", "download plotly graph")
        ),

        # Show a plotly graph 
        mainPanel(
            plotlyOutput("plotlyPlot")
        )
    )

)

server <- function(input, output) {

    session_store <- reactiveValues()

    output$plotlyPlot <- renderPlotly({

        # make a ggplot graph
        g <- ggplot(faithful) + geom_point(aes(x = eruptions, y = waiting))

        # convert the graph to plotly graph and put it in session store
        session_store$plt <- ggplotly(g)

        # render plotly graph
        session_store$plt
    })

    output$download_plotly_widget <- downloadHandler(
        filename = function() {
            paste("data-", Sys.Date(), ".html", sep = "")
        },
        content = function(file) {
            # export plotly html widget as a temp file to download.
            saveWidget(as_widget(session_store$plt), file, selfcontained = TRUE)
        }
    )
}

# Run the application 
shinyApp(ui = ui, server = server)

注意: 在运行代码之前,必须安装 pandoc。 见http://pandoc.org/installing.html

【讨论】:

  • 是的!这正是我所需要的。非常感谢!!
猜你喜欢
  • 2016-03-12
  • 1970-01-01
  • 2013-09-28
  • 2023-04-11
  • 1970-01-01
  • 2018-06-08
  • 2019-10-03
  • 2018-05-15
  • 1970-01-01
相关资源
最近更新 更多