0

I'm trying to download a image from a Shiny App, this image is produced by a DiagrammeR object.

This is the code:

# 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)

This downloads (with errors) a .txt, obviously wrong. I'm trying to download a .png Also I've tried with appshot with no success.

Paula
  • 497
  • 2
  • 8

2 Answers2

1

Here is one solution among many using shiny, you could also bring back the export as png button

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)
Bruno
  • 4,109
  • 1
  • 9
  • 27
  • Thanks! I did this in the content fuction and it worked: ´´´ htmlwidgets::saveWidget(as_widget(input_plot()), "www/diagrama.html", selfcontained = FALSE) webshot(url = "diagrama.html", delay = 5, file = file) ´´´ – Paula Jun 18 '20 at 20:08
  • Ok, please consider upvoting and accepting my answer since my code worked on your reprex – Bruno Jun 18 '20 at 20:30
0

This works:

# 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)
Paula
  • 497
  • 2
  • 8