具有Shiny的VisNetwork的单击事件 [英] Click events for VisNetwork with Shiny
问题描述
我已经使用Shiny中的visNetwork软件包构建了我的网络.我想单击一个节点,然后从数据框中显示有关该节点的信息.我已经能够通过单击和近点功能对散点图进行此操作,例如此处显示的Shiny示例中的功能:
I have built my network with the visNetwork package in Shiny. I would like to click on a node and then display the information about the node from a dataframe. I have been able to do this for scatterplots with the click and nearpoint functions, such as the ones in the Shiny example shown here: http://shiny.rstudio.com/gallery/plot-interaction-selecting-points.html.
对于我的网络,我已经尝试过:
For my network, I have tried:
server <- function(input, output) {
output$network <- renderVisNetwork({
visNetwork(my.nodes, my.edges,
height = "100%", width = "100%",
main = "") %>%
visEvents(hoverNode = "function(nodes){
Shiny.onInputChange('current_node_id',nodes);
;}",
click = "function(){
Shiny.onInputChange('click',{node: current_node_id});
;}"
)
})
output$shiny_return <- renderPrint({
if(!is.null(input$current_node_id)){
nearPoints(node.data,click$node, addDist = TRUE )
}
})
ui <- fluidPage(
visNetworkOutput("network"),
verbatimTextOutput("shiny_return")
)
但是,我收到一条错误消息,提示找不到点击对象"
But, I get an error saying "click object not found"
谢谢您的帮助.
推荐答案
不同点:
- 您的JavaScript事件
click
错误.不了解current_node_id
,必须使用input$click
而不是click$node
闪亮地调用
-
nearPoints
仅适用于plotOuput.不能与javascript/htmlwidgets函数一起使用.
- your javascript event
click
is wrong. Don't know aboutcurrent_node_id
and must be call in shiny withinput$click
and notclick$node
nearPoints
is only for plotOuput. Can't use with javascript / htmlwidgets functions.
要使用visNetwork
启用这种功能,我只是在最新的dev版本中添加了一个新功能visNearestNodes
.这是一个简单的示例:
To enabled this kind of function with visNetwork
, I've just add a new function visNearestNodes
in the latest dev vervion. This is a simple example :
# install dev version
devtools::install_github("datastorm-open/visNetwork")
require(visNetwork)
require(shiny)
nodes <- data.frame(id = 1:15, label = paste("Label", 1:15),
group = sample(LETTERS[1:3], 15, replace = TRUE))
edges <- data.frame(from = trunc(runif(15)*(15-1))+1,
to = trunc(runif(15)*(15-1))+1)
server <- function(input, output, session) {
output$network <- renderVisNetwork({
visNetwork(nodes, edges,
height = "100%", width = "100%",
main = "") %>%
visEvents(click = "function(nodes){
Shiny.onInputChange('click', nodes.nodes[0]);
;}"
)
})
output$shiny_return <- renderPrint({
visNetworkProxy("network") %>%
visNearestNodes(target = input$click)
})
}
ui <- fluidPage(
visNetworkOutput("network"),
verbatimTextOutput("shiny_return")
)
shiny::shinyApp(ui = ui, server = server)
这篇关于具有Shiny的VisNetwork的单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!