0

我有下面的闪亮应用程序,我想从中截取 svg 文件的屏幕截图,但它只捕获它的上角。

library(shiny)
library(DiagrammeR)
library(tidyverse)
# probably don't need all of these:
library(DiagrammeRsvg)
library(svglite)
library(svgPanZoom)
library(rsvg)
library(V8)# only for svg export but also does not work
library(xml2)
library(magrittr)
library(shinyscreenshot)
ui <- fluidPage(
  tags$head(
    tags$script(src = "https://unpkg.com/panzoom@9.4.0/dist/panzoom.min.js")
  ),
  grVizOutput("grr",width = "100%",height = "90vh"),
  actionButton("go", "Take a screenshot"),
  tags$script(
    HTML('panzoom($("#grr")[0])')
  )
)

server <- function(input, output) {
  
  observeEvent(input$go, {
    screenshot(selector="#grr")
  })
  
  reactives <- reactiveValues()
  observe({
    reactives$graph <- render_graph(create_graph() %>%
                                      add_n_nodes(n = 2) %>%
                                      add_edge(
                                        from = 1,
                                        to = 2,
                                        edge_data = edge_data(
                                          value = 4.3)))
  })
  output$grr <-
    renderGrViz(reactives$graph
    )
  
}

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

1 回答 1

2

您可以改用捕获包。效果很好,但您不会得到 SVG 图像。

library(shiny)
library(DiagrammeR)
library(magrittr)
library(capture)

ui <- fluidPage(
  tags$head(
    tags$script(src = "https://unpkg.com/panzoom@9.4.0/dist/panzoom.min.js")
  ),

  grVizOutput("grr", width = "100%", height = "90vh"),

  capture(
    selector = "#grr",
    filename = "myimage.png",
    icon("camera"), "Take screenshot"
  ),

  tags$script(
    HTML('panzoom($("#grr")[0])')
  )
)

server <- function(input, output) {

  reactives <- reactiveValues()
  observe({
    reactives$graph <- render_graph(create_graph() %>%
                                      add_n_nodes(n = 2) %>%
                                      add_edge(
                                        from = 1,
                                        to = 2,
                                        edge_data = edge_data(
                                          value = 4.3)))
  })
  output$grr <- renderGrViz(reactives$graph)

}

# Run the application
shinyApp(ui = ui, server = server)
于 2021-12-16T12:47:49.540 回答