简体   繁体   English

闪亮的Visnetwork互动和事件

[英]Shiny Visnetwork Interactions & Events

I'm trying to create the effect of an interactive legend for a network visualization. 我正在尝试为网络可视化创建交互式图例的效果。 Ideally, I'd like the user to be able to click a legend node and it would highlight/focus in the larger network chart. 理想情况下,我希望用户能够单击图例节点,并且它将在较大的网络图表中突出显示/聚焦。

I have a similar network chart I've been able to use a selectInput drop-down to do the highlight/focus action using something like the snippet below, but I don't know how to pass the values from another network vs a selectInput. 我有一个类似的网络图表,我已经能够使用selectInput下拉列表使用下面的代码片段进行突出显示/聚焦操作,但是我不知道如何从另一个网络与selectInput传递值。

 observe({
    visNetworkProxy("vis_1") %>%
      visFocus(id = input$Focus, scale = 1)%>%
      visSelectNodes(id = input$Focus)
    #  visSetSelection(id = input$Focus, highlightEdges = TRUE)
  })

My thought is to create two network charts (one small one to serve as the legend) and a larger, overall network. 我的想法是创建两个网络图表(一个小图表作为图例)和一个更大的整体网络。 I could then click the legend and zero in on the group in larger chart. 然后,我可以单击图例,并在较大图表的组中将其归零。 Below is sample data to create the first part (legend chart and network chart)... I'm not sure how to pass the click event and the corresponding group. 下面是用于创建第一部分的示例数据(传奇图和网络图)...我不确定如何传递click事件和相应的组。

library(shiny)
library(visNetwork)
library(DT)

server <- function(input, output, session) {
  ## data
  nodes <- data.frame(id = 1:3, 
                      name = c("first", "second", "third"), 
                      group = c("info1", "info1", "info2"),
                      color = c("blue","blue","red"))
  edges <- data.frame(from = c(1,2), to = c(2,2), id = 1:2)

  ## data for legend network  
  nodesb <- data.frame(id = c("info1","info2"),
                       color = c("blue","red"))


  ##  network
  output$network_proxy1 <- renderVisNetwork({
    visNetwork(nodes, edges, main = "Network Chart") %>%
    visEvents(select = "function(nodes) {
                Shiny.onInputChange('current_node_id', nodes.nodes);
                ;}")
    })
  ## legend network
  output$network_proxy2 <- renderVisNetwork({
    visNetwork(nodesb, main = "Legend") %>%
    visEvents(select = "function(nodes) {
                Shiny.onInputChange('current_node_id', nodes.nodes);
                ;}")
    })



}

ui <- fluidPage(
  visNetworkOutput("network_proxy2", height = "100px"),
  visNetworkOutput("network_proxy1", height = "400px")
)

shinyApp(ui = ui, server = server)

You almost had it. 你差点就吃了。 You can reference Shiny.onInputChange values in your server function, treating it as any other input. 您可以在服务器函数中引用Shiny.onInputChange值,将其视为任何其他输入。 Here is how that will look: 外观如下:

library(shiny)
library(visNetwork)
library(DT)
library(dplyr)

server <- function(input, output, session) {
  ## data
  nodes <- data.frame(id = 1:3, 
                      name = c("first", "second", "third"), 
                      group = c("info1", "info1", "info2"),
                      color = c("blue","blue","red"))
  edges <- data.frame(from = c(1,2), to = c(2,2), id = 1:2)

  ## data for legend network  
  nodesb <- data.frame(id = c("info1","info2"),
                       color = c("blue","red"))


  ##  network
  output$network_proxy1 <- renderVisNetwork({
    visNetwork(nodes, edges, main = "Network Chart")
})
  ## legend network
  output$network_proxy2 <- renderVisNetwork({
    visNetwork(nodesb, main = "Legend") %>%
      visEvents(select = "function(nodes) {
                Shiny.onInputChange('current_node_id_legend', nodes.nodes);
                ;}")
})

  # Find the ID of the gorup selected and focus on the first element
  observe({

    id = nodes%>%
      filter(group %in% input$current_node_id_legend)%>%
      .$id%>%
      .[1]

    visNetworkProxy("network_proxy1") %>%
      visFocus(id = id, scale = 4)
  })

}

ui <- fluidPage(
  visNetworkOutput("network_proxy2", height = "100px"),
  visNetworkOutput("network_proxy1", height = "400px")
)

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM