将 DiagrammeR,data.tree 导出到 Shiny 中的图像 (png)

Export DiagrammeR, data.tree to image (png) in Shiny

我正在尝试从 Shiny App 下载图像,该图像是由 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 我也尝试过使用 appshot 但没有成功。

这是许多使用 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)

这个有效:

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