Shiny 中带有 VisNetwork Graph 的搜索栏

Search bar with VisNetwork Graph in Shiny

我有一个 uilt 网络图,其中包含 VisNetwork 和 Shiny。我对结果非常满意。我想做的是使用搜索栏(例如:http://projects.flowingdata.com/tut/interactive_network_demo/)来搜索我数据中的节点。

我正在使用 shinydashboard。所以我尝试使用"sidebarSearchForm"。但是,当我 运行 应用程序并尝试使用搜索表单时,没有返回任何内容。

这是我的 ui 代码:

ui <- dashboardPage(skin = "black",
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Network", tabName = "network", icon = icon("dashboard")),
      sidebarSearchForm(textId = "searchText", buttonId = "searchButton", label = "Search...")
   )
  ),
  dashboardBody(
    box(
      title = "Network",  status = "warning", solidHeader = TRUE, collapsible = TRUE,
      visNetworkOutput("network_proxy", height = 700)  
    )
  )
)#end ui

这是服务器的代码;

server <- function(input, output) {
  output$network_proxy <- renderVisNetwork({
    visNetwork(my.nodes, my.edges, height = "100%")
  })
  output$searchString <- renderText({
    if (input$searchButton == 0)
      return()
    isolate({input$searchString})
  })
} #end server

你检查过了吗:

http://datastorm-open.github.io/visNetwork/shiny.html

检查 visNetworkProxy 部分。

另外,包里自带的演示代码有你要实现的东西。

例如,您可以使用 visNetworkProxyvisSelectNodes 来执行此操作,就像这样使用简单的 grepl

nodes <- data.frame(id = 1:3, label = c("A", "B", "A"))
edges <- data.frame(from = c(1,2), to = c(1,3))

require(visNetwork)
require(shiny)
require(shinydashboard)
ui <- dashboardPage(skin = "black",
                    dashboardHeader(),
                    dashboardSidebar(
                      sidebarMenu(
                        menuItem("Network", tabName = "network", icon = icon("dashboard")),
                        sidebarSearchForm(textId = "searchText", buttonId = "searchButton", label = "Search...")
                      )
                    ),
                    dashboardBody(
                      box(
                        title = "Network",  status = "warning", solidHeader = TRUE, collapsible = TRUE,
                        visNetworkOutput("network_proxy", height = 700)
                      )
                    )
)


server <- function(input, output, session) {
  output$network_proxy <- renderVisNetwork({
    visNetwork(nodes, edges, height = "100%")
  })

  observe({
    if(input$searchButton > 0){
      isolate({
        print(input$searchText)
        current_node <- nodes[grep(input$searchText, nodes$label), "id"]
        print(current_node)
        visNetworkProxy("network_proxy") %>% visSelectNodes(id  = current_node)
      })
    }
  })

} #end server

shiny::shinyApp(ui, server)

以上来自 bthieurmel 的代码可以修改为以下代码以启用不区分大小写的搜索:

current_node <- nodes[grep(input$searchText, nodes$label, ignore.case = T), "id"]