Экспорт DiagrammeR, data.tree в изображение (png) в Shiny - PullRequest
0 голосов
/ 18 июня 2020

Я пытаюсь загрузить изображение из приложения Shiny, это изображение создается объектом DiagrammeR.

Это код:

# Load packages
library(shinythemes)
library(DiagrammeR)
library(data.tree)
library(plotly)
library(shiny)

# Load data
data(acme)

# Define UI
ui <- fluidPage(theme = shinytheme("lumen"),
                titlePanel("Paula trying II"),
                sidebarLayout(
                  sidebarPanel(downloadButton(outputId = "dld_diagrama", label = "Download diagram")),
                  mainPanel(
                    grVizOutput("tree_plot", width = "100%", height = "760px")
                  )
                )
)

# Define server function
server <- function(input, output) {

  output$tree_plot <- renderGrViz({

    plot(acme) 

  })  

output$dld_diagrama <- downloadHandler(
    filename = function(){

      paste("diagram", "png", sep = ".")
    },
    content = function(file) {
      plotly::export(tree_plot, file = "diagram.png")
    }

)

}

# Create Shiny object
shinyApp(ui = ui, server = server)

Это загружает (с ошибками) .txt, что явно неверно. Я пытаюсь загрузить .png. Я также безуспешно пытался использовать приложение со снимком.

Ответы [ 2 ]

1 голос
/ 18 июня 2020

Вот одно из многих решений, использующих shiny, вы также можете вернуть экспорт как кнопку png

library(shinythemes)
library(DiagrammeR)
library(data.tree)
library(plotly)
library(shiny)

data(acme)

# Define UI
ui <- fluidPage(theme = shinytheme("lumen"),
                titlePanel("Paula trying II"),
                sidebarLayout(
                  sidebarPanel(downloadButton(outputId = "dld_diagrama", label = "Download diagram")),
                  mainPanel(
                    grVizOutput("tree_plot", width = "100%", height = "760px")
                  )
                )
)

# Define server function
server <- function(input, output) {

  input_plot <- reactive(plot(acme))

  output$tree_plot <- renderGrViz({

    input_plot() 

  })  

  output$dld_diagrama <- downloadHandler(
    filename = function(){

      paste("diagram", "html", sep = ".")
    },
    content = function(file) {
      htmlwidgets::saveWidget(as_widget(input_plot()), file)
    }

  )

}

# Create Shiny object
shinyApp(ui = ui, server = server)
0 голосов
/ 18 июня 2020

Это работает:

# Load packages
library(shinythemes)
library(DiagrammeR)
library(data.tree)
library(plotly)
library(shiny)

# Load data
data(acme)

# Define UI
ui <- fluidPage(theme = shinytheme("lumen"),
                titlePanel("Paula trying II"),
                sidebarLayout(
                  sidebarPanel(downloadButton(outputId = "dld_diagrama", label = "Download diagram")),
                  mainPanel(
                    grVizOutput("tree_plot", width = "100%", height = "760px")
                  )
                )
)

# Define server function
server <- function(input, output) {

input_plot <- reactive(plot(acme))

  output$tree_plot <- renderGrViz({

    plot(acme) 

  })  

output$dld_diagrama <- downloadHandler(
    filename = function(){

      paste("diagram", "png", sep = ".")
    },
    content = function(file) {
       htmlwidgets::saveWidget(as_widget(input_plot()), "www/diagrama.html", selfcontained = FALSE) 
       webshot(url = "diagrama.html", delay = 5, file = file) 
    }

)

}

# Create Shiny object
shinyApp(ui = ui, server = server)
...