从visNetwork图获取节点和边缘数据 [英] Get Node and Edge data from visNetwork graph
问题描述
我试图从我的visNetwork图中获取节点/边缘数据。我正在使用示例代码,但它不起作用。我正在试着用Shiny来做到这一点。我的目标是从网络获取节点和边缘数据,然后将其显示在表格中。我将非常感谢任何帮助,我可以得到。
谢谢,
I am trying to get the nodes/edges data from my visNetwork graph. I am using the example code, but it is not working. I am trying to do this with Shiny. My goal is to get the nodes and edges data from the network and then display it in a table. I will greatly appreciate any help that I can get. Thanks,
这是我的代码:
Here is my code:
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)
推荐答案
代码的主要问题是网络输出$ network_proxy_nodes
的名称是到处都不一样。
The main problem with the code is that the name of the network output$network_proxy_nodes
was not the same everywhere.
这是一段代码,用于获取边缘信息并以纯文本形式显示它,并获取节点信息并将其显示为DataTable:
This is a working piece of code to get edge information and display it in plain text, and to get node information and display it as a 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)
这篇关于从visNetwork图获取节点和边缘数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!