从没有操作按钮的visNetwork图中获取选定的节点数据 [英] Get selected Node data from visNetwork graph without actionButton
问题描述
我使用了此问题中给出的示例,但我想调整它以显示已经选择的节点的数据(不是所有的节点,但只有这个节点的数据),也不使用动作按钮,这样一旦我点击节点,数据就会移动。 / p>
我尝试了许多解决方案,但没有成功。
创建图表时,我上传CSV文件并关联节点的其他参数(大小,标题...)。当我选择一个节点时,是否可以显示这些参数:
节点$ company_name
节点$ company_postcode
节点$ amount.size
...
以下是我用于开始的代码,由@xclotet给出
require(闪亮)$ b $ (输入,输出,会话){
节点< - 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)
输出$ network_proxy< - renderVisNetwork({
visNetwork(nodes,edges)
})
输出$ nodes_data_from_shiny< - renderDataTable({
if(!is.null(input $ network_proxy_nodes)){
info< - data.frame(矩阵(unlist(input $ network_proxy_nodes),ncol = dim(节点)[1]),
(节点)
信息
}
})
observeEvent(输入$ getNodes,{
visNetworkProxy(network_proxy)%>%
visGetNodes()
})
}
ui< - fluidPage (
visNetworkOutput(network_proxy,height =400px),
dataTableOutput(nodes_data_from_shiny),
actionButton(getNodes,节点)
)
shinyApp(ui = ui,server = server)
要显示所选节点的数据,可以修改中给出的示例visNetwork闪亮网页。在这个例子中, visEvents
的 hoverNode
选项用于获取 hovered 节点的信息。
要获取选定的节点ID ,可以使用:
visEvents(select =function(nodes){
。然后,您可以使用此信息仅显示与该节点相对应的信息(通过数据框架的子集)。
Shiny.onInputChange('current_node_id',nodes.nodes);
;})
$ c这个函数将节点(nodes.nodes
)的id设置为<$ c $ <$ pre>
C $ C>输入$ current_node_id
下面提供的示例适用于回答以下问题:
需要(有光泽)
require(visNetwork)
服务器< - 函数(输入,输出,会话){
节点< - 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)
输出$ network_proxy< - renderVisNetwork({
visNetwork(节点,边)%>%
visEvents(select =function(nodes){
Shiny.onInputChange('current_node_id',nodes.nodes);
;})$ (
))
output $ nodes_data_from_shiny< - renderDataTable({
if(!is.null(input $ current_node_id)&&!is.null(input $ network_proxy_nodes) ){
info< - data.frame(matrix(unlist(input $ network_proxy_nodes),
ncol = dim(nodes) (信息)< - colnames(节点)
信息[信息$ id ==输入$ current_node_id,]
$ b observeEvent(输入$ current_node_id,{
visNetworkProxy(network_proxy)%>%
visGetNodes()
} )
$ b ui< - fluidPage(
visNetworkOutput(network_proxy,height =400px),
dataTableOutput(nodes_data_from_shiny ),
actionButton(getNodes,Nodes)
)
shinyApp(ui = ui,server = server)
I used the example given in this question but I would like to adapt it to display the data of the node that has been selected (not all the node but only this one) and also not use the action button so that the data are displaid as soon as I click on the node.
I have tried many solution without success.
When I create my graph, I upload a CSV file and associate other parameters to the nodes (size, title ...). Would it be possible to display these parameters also when I select a node :
nodes$company_name
nodes$company_postcode
nodes$amount.size
...
Here is the code I used to start with, kindly given by @xclotet
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 } }) observeEvent(input$getNodes,{ visNetworkProxy("network_proxy") %>% visGetNodes() }) } ui <- fluidPage( visNetworkOutput("network_proxy", height = "400px"), dataTableOutput("nodes_data_from_shiny"), actionButton("getNodes", "Nodes") ) shinyApp(ui = ui, server = server)
解决方案To display data of the node selected, you can adapt the example given in visNetwork Shiny webpage. In that example,
hoverNode
option ofvisEvents
is used to get information of the hovered node.To get the selected node id one can use:
visEvents(select = "function(nodes) { Shiny.onInputChange('current_node_id', nodes.nodes); ;}")
This function sets the id of the node (
nodes.nodes
) toinput$current_node_id
. Then, you can use this information to display only the information corresponding to that node (by subsetting the data.frame).Below, the example provided adapted to answer the question:
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) %>% visEvents(select = "function(nodes) { Shiny.onInputChange('current_node_id', nodes.nodes); ;}") }) output$nodes_data_from_shiny <- renderDataTable( { if (!is.null(input$current_node_id) && !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[info$id == input$current_node_id, ] } }) observeEvent(input$current_node_id, { visNetworkProxy("network_proxy") %>% visGetNodes() }) } ui <- fluidPage( visNetworkOutput("network_proxy", height = "400px"), dataTableOutput("nodes_data_from_shiny"), actionButton("getNodes", "Nodes") ) shinyApp(ui = ui, server = server)
这篇关于从没有操作按钮的visNetwork图中获取选定的节点数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!