我正在Shiny应用程序中进行网络分析。
我想使用 visNetworkProxy 功能基于节点ID 来交互(聚焦/选择)节点。
但是,UI中的selectInput中的“ nodes $ id” 必须预定义。在这种情况下,我必须定义服务器外部的节点和边缘,而不是服务器内部。
由于我的项目性质,我必须保持服务器中定义的节点和边缘,以使其随数据库更新。
下面是我的代码:
server <- function(input, output) {
output$network_proxy_nodes <- renderVisNetwork({
# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges) %>% visNodes(color = "blue")
})
observe({
visNetworkProxy("network_proxy_nodes") %>%
visFocus(id = input$Focus, scale = 4)
})
observe({
visNetworkProxy("network_proxy_nodes") %>%
visNodes(color = input$color)
})
}
ui <- fluidPage(
fluidRow(
column(
width = 4,
selectInput("color", "Color :",
c("blue", "red", "green")),
selectInput("Focus", "Focus on node :",
nodes$id)
),
column(
width = 8,
visNetworkOutput("network_proxy_nodes", height = "400px")
)
)
)
shinyApp(ui = ui, server = server)
我想知道是否有一种方法可以与具有节点$ id的节点进行交互,同时保留服务器内部的节点和边缘。
谢谢!
答案 0 :(得分:1)
这将起作用。您需要根据节点动态呈现selectInput
。
library(shiny)
server <- function(input, output) {
# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
output$network_proxy_nodes <- renderVisNetwork({
visNetwork(nodes, edges) %>% visNodes(color = "blue")
})
observeEvent(input$Focus, {
visNetworkProxy("network_proxy_nodes") %>%
visFocus(id = input$Focus, scale = 4)
})
observeEvent(input$color, {
visNetworkProxy("network_proxy_nodes") %>%
visNodes(color = input$color)
})
output$choose_node <- renderUI({
selectInput("Focus", "Focus on node :",
nodes$id)
})
}
ui <- fluidPage(
fluidRow(
column(
width = 4,
selectInput("color", "Color :",
c("blue", "red", "green"))
),
column(
width = 8,
visNetworkOutput("network_proxy_nodes", height = "400px"),
uiOutput("choose_node")
)
)
)
shinyApp(ui = ui, server = server)
编辑
在评论之后,添加一个按钮。
library(shiny)
server <- function(input, output) {
# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
output$network_proxy_nodes <- renderVisNetwork({
visNetwork(nodes, edges) %>% visNodes(color = "blue")
})
observeEvent(input$focus_now, {
visNetworkProxy("network_proxy_nodes") %>%
visFocus(id = input$Focus, scale = 4)
})
observeEvent(input$color, {
visNetworkProxy("network_proxy_nodes") %>%
visNodes(color = input$color)
})
output$choose_node <- renderUI({
selectInput("Focus", "Focus on node :",
nodes$id)
})
}
ui <- fluidPage(
fluidRow(
column(
width = 4,
selectInput("color", "Color :",
c("blue", "red", "green"))
),
column(
width = 8,
visNetworkOutput("network_proxy_nodes", height = "400px"),
uiOutput("choose_node"),
actionButton("focus_now", "FOCUS")
)
)
)
shinyApp(ui = ui, server = server)