Как экспортировать ggplotly из R блестящего приложения в виде файла HTML - PullRequest
1 голос
/ 17 апреля 2019

У меня в блестящем приложении есть объект ggplotly с именем p2.Я хочу, чтобы это работало, когда пользователь нажимает кнопку downloadButton в пользовательском интерфейсе приложения, он будет загружать график ggplotly в виде html-файла.Но функция не работает:

output$Export_Graph <- downloadHandler(

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

      },

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

Есть идеи?

1 Ответ

1 голос
/ 18 апреля 2019

Интерактивный граф график может быть экспортирован как «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

...