我可以双击 visNetwork 图中的节点以 运行 函数吗?
Can I double click on a node in a visNetwork diagram to run a function?
我有一个包含几个节点的网络图,每个节点都有一些数据,包括 ID 及其名称。
我正在像这样构建 visNetwork 对象:
getDiagramPlot <- function(nodes, edges){
v <- visNetwork(
nodes,
edges
) %>%
visPhysics(stabilization = TRUE, enabled = TRUE) %>%
visOptions(highlightNearest = list(enabled = T, degree = 1, hover = F), autoResize = TRUE, collapse = FALSE) %>%
visEdges(color = list(highlight = "red")) %>% # The colour of the edge linking nodes
visLayout(improvedLayout = TRUE) %>%
visEdges(arrows = edges$arrows) %>%
visInteraction(multiselect = F)
return(v)
}
我想要的是能够通过管道输入 visEvents
并在我的代码中调用函数,理想情况下将 ID 作为参数传递。类似于:
testFunction <- function(node_id){
print(paste("The selected node ID is:", node_id))
}
我在网上看到的例子大多在他们的例子中使用 javascript alert()
,但我希望突破 javascript 并在其中调用 R 函数我的代码。
如有任何帮助,我们将不胜感激!先感谢您。
您可以在 javascript 中使用 Shiny.onInputChange
将任何内容设置为 Shiny 输入变量。这样就可以了。
编辑:在visEvents中使用doubleClick
双击触发代码。参见 https://rdrr.io/cran/visNetwork/man/visEvents.html
library(shiny)
library(visNetwork)
ui <- fluidPage(
visNetworkOutput('network')
)
server <- function(input, output, session) {
getDiagramPlot <- function(nodes, edges){
v <- visNetwork(
nodes,
edges
) %>%
visPhysics(stabilization = TRUE, enabled = TRUE) %>%
visOptions(highlightNearest = list(enabled = T, degree = 1, hover = F), autoResize = TRUE, collapse = FALSE) %>%
visEdges(color = list(highlight = "red")) %>% # The colour of the edge linking nodes
visLayout(improvedLayout = TRUE) %>%
visEdges(arrows = edges$arrows) %>%
visInteraction(multiselect = F) %>%
visEvents(doubleClick = "function(nodes) {
Shiny.onInputChange('current_node_id', nodes.nodes);
;}")
return(v)
}
testFunction <- function(node_id){
print(paste("The selected node ID is:", node_id))
}
nodes <- data.frame(id = 1:3, label = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
output$network <- renderVisNetwork(
getDiagramPlot(nodes, edges)
)
observeEvent(input$current_node_id,{
testFunction(input$current_node_id)
})
}
shinyApp(ui, server)
我有一个包含几个节点的网络图,每个节点都有一些数据,包括 ID 及其名称。 我正在像这样构建 visNetwork 对象:
getDiagramPlot <- function(nodes, edges){
v <- visNetwork(
nodes,
edges
) %>%
visPhysics(stabilization = TRUE, enabled = TRUE) %>%
visOptions(highlightNearest = list(enabled = T, degree = 1, hover = F), autoResize = TRUE, collapse = FALSE) %>%
visEdges(color = list(highlight = "red")) %>% # The colour of the edge linking nodes
visLayout(improvedLayout = TRUE) %>%
visEdges(arrows = edges$arrows) %>%
visInteraction(multiselect = F)
return(v)
}
我想要的是能够通过管道输入 visEvents
并在我的代码中调用函数,理想情况下将 ID 作为参数传递。类似于:
testFunction <- function(node_id){
print(paste("The selected node ID is:", node_id))
}
我在网上看到的例子大多在他们的例子中使用 javascript alert()
,但我希望突破 javascript 并在其中调用 R 函数我的代码。
如有任何帮助,我们将不胜感激!先感谢您。
您可以在 javascript 中使用 Shiny.onInputChange
将任何内容设置为 Shiny 输入变量。这样就可以了。
编辑:在visEvents中使用doubleClick
双击触发代码。参见 https://rdrr.io/cran/visNetwork/man/visEvents.html
library(shiny)
library(visNetwork)
ui <- fluidPage(
visNetworkOutput('network')
)
server <- function(input, output, session) {
getDiagramPlot <- function(nodes, edges){
v <- visNetwork(
nodes,
edges
) %>%
visPhysics(stabilization = TRUE, enabled = TRUE) %>%
visOptions(highlightNearest = list(enabled = T, degree = 1, hover = F), autoResize = TRUE, collapse = FALSE) %>%
visEdges(color = list(highlight = "red")) %>% # The colour of the edge linking nodes
visLayout(improvedLayout = TRUE) %>%
visEdges(arrows = edges$arrows) %>%
visInteraction(multiselect = F) %>%
visEvents(doubleClick = "function(nodes) {
Shiny.onInputChange('current_node_id', nodes.nodes);
;}")
return(v)
}
testFunction <- function(node_id){
print(paste("The selected node ID is:", node_id))
}
nodes <- data.frame(id = 1:3, label = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
output$network <- renderVisNetwork(
getDiagramPlot(nodes, edges)
)
observeEvent(input$current_node_id,{
testFunction(input$current_node_id)
})
}
shinyApp(ui, server)