我想在选择节点时显示节点标签,并且仅在选择它们时显示。这是我一直在尝试的,但问题是一旦我选择了一个或多个节点,标签确实会出现,但是当我取消选择它们以选择其他节点时它会卡住并且不会再消失。
library(shiny)
library(visNetwork)
ui <- fluidPage(
visNetworkOutput("network"),
selectInput("selectedNodes", "", choices = c("",1:3), multiple = TRUE)
)
server <- function(input, output, session) {
output$network <- renderVisNetwork({
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges) %>% visInteraction(multiselect = T)
})
observe({
req(input$selectedNodes)
selected_ids = input$selectedNodes
visNetworkProxy("network") %>%
visUpdateNodes(nodes = data.frame(id=selected_ids,label=paste("Label",selected_ids)))
})
observe({
print(input$network_selectedNodes)
})
}
shinyApp(ui = ui, server = server)
感谢您的帮助 !