在Shiny中使用VisNetwork Graph搜索栏

时间:2016-10-13 15:18:30

标签: r graph shiny shinydashboard

我已经使用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

2 个答案:

答案 0 :(得分:2)

您可以使用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)

答案 1 :(得分:0)

你看看这个:

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

检查visNetworkProxy部分。

此外,演示代码附带包含您想要实现的东西。