【问题标题】:How to use visNetworkProxy in Shiny to interact with Nodes based on Nodes ids如何在 Shiny 中使用 visNetworkProxy 根据节点 id 与节点交互
【发布时间】:2019-07-19 12:58:16
【问题描述】:

我正在 Shiny 应用中构建网络分析。

我想使用 visNetworkProxy 函数根据节点 ID交互(聚焦/选择)节点

但是,UI 中 selectInput 中的 “nodes$id” 必须是预定义的。在这种情况下,我必须在服务器外部定义节点和边,而不是在服务器内部。

由于我项目的性质,我必须保留在服务器中定义的节点和边,以使它们与数据库保持更新。

下面是我的代码:

  server <- function(input, output) {
  output$network_proxy_nodes <- renderVisNetwork({
    # minimal example
    nodes <- data.frame(id = 1:3)
    edges <- data.frame(from = c(1,2), to = c(1,3))

    visNetwork(nodes, edges) %>% visNodes(color = "blue")
  })


  observe({
    visNetworkProxy("network_proxy_nodes") %>%
      visFocus(id = input$Focus, scale = 4)
  })

  observe({
    visNetworkProxy("network_proxy_nodes") %>%
      visNodes(color = input$color)
  })

}

ui <- fluidPage(
  fluidRow(
    column(
      width = 4,
      selectInput("color", "Color :",
                  c("blue", "red", "green")),
      selectInput("Focus", "Focus on node :",
                  nodes$id)
    ),
    column(
      width = 8,
      visNetworkOutput("network_proxy_nodes", height = "400px")
    )
  )
)

shinyApp(ui = ui, server = server)

我想知道是否有一种方法可以与具有nodes$id 的节点交互,同时将节点和边缘保留在服务器内

提前致谢!

【问题讨论】:

    标签: javascript r shiny rstudio visnetwork


    【解决方案1】:

    这是可行的。您需要根据节点动态渲染selectInput

    library(shiny)
    
     server <- function(input, output) {
    
      # minimal example
      nodes <- data.frame(id = 1:3)
      edges <- data.frame(from = c(1,2), to = c(1,3))
    
      output$network_proxy_nodes <- renderVisNetwork({
        visNetwork(nodes, edges) %>% visNodes(color = "blue")
      })
    
    
      observeEvent(input$Focus, {
        visNetworkProxy("network_proxy_nodes") %>%
          visFocus(id = input$Focus, scale = 4)
      })
    
      observeEvent(input$color, {
        visNetworkProxy("network_proxy_nodes") %>%
          visNodes(color = input$color)
      })
    
      output$choose_node <- renderUI({
        selectInput("Focus", "Focus on node :",
                    nodes$id)
      })
    
    }
    
    ui <- fluidPage(
      fluidRow(
        column(
          width = 4,
          selectInput("color", "Color :",
                      c("blue", "red", "green"))
        ),
        column(
          width = 8,
          visNetworkOutput("network_proxy_nodes", height = "400px"),
          uiOutput("choose_node")
        )
      )
    )
    
    shinyApp(ui = ui, server = server)
    

    编辑

    根据您的评论,添加一个按钮。

    library(shiny)
    
     server <- function(input, output) {
    
      # minimal example
      nodes <- data.frame(id = 1:3)
      edges <- data.frame(from = c(1,2), to = c(1,3))
    
      output$network_proxy_nodes <- renderVisNetwork({
        visNetwork(nodes, edges) %>% visNodes(color = "blue")
      })
    
    
      observeEvent(input$focus_now, {
        visNetworkProxy("network_proxy_nodes") %>%
          visFocus(id = input$Focus, scale = 4)
      })
    
      observeEvent(input$color, {
        visNetworkProxy("network_proxy_nodes") %>%
          visNodes(color = input$color)
      })
    
      output$choose_node <- renderUI({
        selectInput("Focus", "Focus on node :",
                    nodes$id)
      })
    
    }
    
    ui <- fluidPage(
      fluidRow(
        column(
          width = 4,
          selectInput("color", "Color :",
                      c("blue", "red", "green"))
        ),
        column(
          width = 8,
          visNetworkOutput("network_proxy_nodes", height = "400px"),
          uiOutput("choose_node"),
          actionButton("focus_now", "FOCUS")
        )
      )
    )
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 谢谢约翰!这对我来说效果很好。只是有 1 个问题:通过应用您的代码,当我运行应用程序时,应用程序将自动关注默认节点(本例中为 1)。但是,即使默认节点 ID 在选择框中,我希望应用程序在打开它时不要关注任何节点。你对此有什么想法吗?非常感谢!
    • 您可以添加actionButton 并让节点专注于点击吗?
    • 是的,添加一个 actionButton 是个好主意。但是我有点困惑,为什么在我的原始代码中添加“choose_node”输出后它会自动关注默认节点。我想知道是否有办法在不添加 acionButton 的情况下解决这个问题。这只是出于好奇,如果没有其他选择,可以使用操作按钮。非常感谢!
    • 您可以在 selectInput 中添加一个选项说“无焦点”。并在代理中添加一个 if 语句以不关注 if ...(上面使用 actionButton 编辑的代码)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 2021-12-18
    相关资源
    最近更新 更多