2016-10-06 11 views
1

visNetworkグラフからノード/エッジデータを取得しようとしています。私はサンプルコードを使用していますが、動作していません。私はシャイニーでこれをやろうとしています。私の目標は、ネットワークからノードとエッジのデータを取得し、それをテーブルに表示することです。私は私が得ることができるどんな助けにも大いに感謝します。visNetworkグラフからノードとエッジデータを取得

require(shiny) 
require(visNetwork) 

server <- function(input, output) { 

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

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


    output$edges_data_from_shiny <- renderPrint({ 
    if(!is.null(input$network_proxy_get_edges)){ 
     input$network_proxy_get_edges 
    } 
    }) 

    observe({ 
    input$getEdges 
    visNetworkProxy("network_proxy_get") %>% 
     visGetEdges() 
    }) 

    output$nodes_data_from_shiny <- renderPrint({ 
    if(!is.null(input$network_proxy_get_nodes)){ 
     input$network_proxy_get_nodes 
    } 
    }) 

    observe({ 
    input$getNodes 
    visNetworkProxy("network_proxy_get") %>% 
     visGetNodes() 
    }) 
} 

ui <- fluidPage(
    visNetworkOutput("network_proxy_nodes", height = "100%"), 
      verbatimTextOutput("edges_data_from_shiny "), 
      verbatimTextOutput("nodes_data_from_shiny"), 
      actionButton("getNodes", "Nodes"), 
      actionButton("getEdges", "Edges") 
) 

shinyApp(ui = ui, server = server) 

答えて

2

コードの主な問題は、ネットワークoutput$network_proxy_nodesの名前はどこでも同じではなかったということです: おかげで、

は、ここに私のコードです。

これは、エッジ情報を取得し、プレーンテキストで表示し、ノード情報を取得したDataTableとして、それを表示するためのコードの作業部分です:

require(shiny) 
require(visNetwork) 

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

    output$network_proxy <- renderVisNetwork({ 
    visNetwork(nodes, edges) 
    }) 


    output$nodes_data_from_shiny <- renderDataTable({ 
    if(!is.null(input$network_proxy_nodes)){ 
     info <- data.frame(matrix(unlist(input$network_proxy_nodes), ncol = dim(nodes)[1], 
         byrow=T),stringsAsFactors=FALSE) 
     colnames(info) <- colnames(nodes) 
     info 
    } 
    }) 
    output$edges_data_from_shiny <- renderPrint({ 
    if(!is.null(input$network_proxy_edges)){ 
     input$network_proxy_edges 
    } 
    }) 

    observeEvent(input$getNodes,{ 
    visNetworkProxy("network_proxy") %>% 
     visGetNodes() 
    }) 

    observeEvent(input$getEdges, { 
    visNetworkProxy("network_proxy") %>% 
     visGetEdges() 
    }) 
} 

ui <- fluidPage(
    visNetworkOutput("network_proxy", height = "400px"), 
    verbatimTextOutput("edges_data_from_shiny"), 
    dataTableOutput("nodes_data_from_shiny"), 
    actionButton("getEdges", "Edges"), 
    actionButton("getNodes", "Nodes") 
) 

shinyApp(ui = ui, server = server) 
+0

どうもありがとう!私はこのコードを動作させることができました。最高、SB – AquieJo

+0

ようこそ@AquieJo!それがあなたのために働いていれば、あなたはそれを「最善の答え」として受け入れることができます(答えの左側に緑色のダニが現れ、他の人はあなたの質問に答えます)。 – xclotet

関連する問題