【问题标题】:How to add controls in pan&zoom functionality in shiny app?如何在闪亮应用程序的平移和缩放功能中添加控件?
【发布时间】:2022-01-19 15:52:13
【问题描述】:

我使用panzoom 包来平移和缩放我闪亮的应用程序中的 svg 文件。有没有办法拥有像this 这样的控件?

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

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

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

  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)

}

shinyApp(ui, server)

【问题讨论】:

标签: r shiny zooming pan


【解决方案1】:

这是一种方法,但如果您在 +/- 按钮上单击过快,则会产生不良影响。

library(shiny)
library(shinyWidgets)
library(DiagrammeR)
library(magrittr)

js <- '
$(document).ready(function(){
  var element = document.getElementById("grr");
  var instance = panzoom(element);
  $("#zoomout").on("click", function(){
    instance.smoothZoom(0, 0, 0.9);
  });
  $("#zoomin").on("click", function(){
    instance.smoothZoom(0, 0, 1.1);
  });
});
'

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

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

  actionGroupButtons(
    inputIds = c("zoomout", "zoomin"),
    labels = list(icon("minus"), icon("plus")),
    status = "primary"
  )

)

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)

}

shinyApp(ui, server)

编辑

添加此 JavaScript 以防止不良影响:

  $("#zoomout").on("dblclick", function(){
    return false;
  });
  $("#zoomin").on("dblclick", function(){
    return false;
  });

【讨论】:

猜你喜欢
  • 2022-01-18
  • 2018-03-03
  • 2022-01-20
  • 2017-07-27
  • 2011-01-04
  • 2021-03-17
  • 1970-01-01
  • 2011-07-17
  • 2020-06-16
相关资源
最近更新 更多