datastorm-open / visNetwork

R package, using vis.js library for network visualization
Other
544 stars 126 forks source link

getVisNodes changes node colors when "group" is included in nodes data #429

Open bjcarothers opened 2 years ago

bjcarothers commented 2 years ago

Using an action button to trigger getVisNodes causes the nodes to change color and a few other selections to malfunction when there is a "group" field in the nodes data.

library(shiny)
library(visNetwork)

# generate data
nodesDf <- data.frame(id=as.character(c(1:5)),
                      label=as.character(c(1:5)),
                      #something=c("one","one","one","two","two"), # this works 
                      group=c("one","one","one","two","two"), # this causes problems
                      color=c("#e41a1c","#e41a1c","#e41a1c","#377eb8","#377eb8")
)
edgesDf <- data.frame(from=as.character(c(1,1,1,1,2,4)),
                      to=as.character(c(2,3,4,5,3,5)))

# ui
ui <- fluidPage(
  visNetworkOutput("netPlot"),
  actionButton(
    inputId="action",
    label="Click Me")
)

# server
server <- function(input, output) {
  output$netPlot <- renderVisNetwork({
    visNetwork(nodesDf, edgesDf) %>%
      visOptions(nodesIdSelection=TRUE, 
                 #selectedBy="something", # this works
                 selectedBy="group", # this does not
                 highlightNearest=list(
                   enabled=TRUE, labelOnly=FALSE)
      )
  })

  observeEvent(input$action,{
    visNetworkProxy("netPlot") %>%
      visGetNodes()
  })
}

shinyApp(ui = ui, server = server)

Here's the vis when first initialized, with everything working fine:

Render1

Selection works fine:

Render2

And the color change after the action button is clicked and the previously greyed-out nodes have color again: Render3

As noted in the data generation above, calling a grouping variable something other than "group" is the workaround. Using "group" as a variable name worked with previous versions and I suspect may be a common thing to do, especially since "group" is used in the documentation examples, (I'll admit it took a long time to suss out the problem after updating the package). Is it possible to restore that functionality?