How to keep a forceNetwork {networkD3} centered in a Shiny app?

前端 未结 1 1192
小蘑菇
小蘑菇 2021-02-10 09:29

While building a Shiny app that displays a forceNetwork graph, the network does not stay centered but moves out of view when one interactively changes the opacity.

My qu

相关标签:
1条回答
  • 2021-02-10 09:57

    The problem is that the timer in d3.force() is running out, and it isn't "reheated" when new values are input. Without changes to networkD3, you can hack this to work by adding colourScale = JS('force.alpha(1); force.restart(); d3.scaleOrdinal(d3.schemeCategory20);') to your arguments in your forceNetwork() function. So you would have for server.R...

    library(shiny)
    library(networkD3)
    
    shinyServer(function(input, output) {
    
           # Load data
           data(MisLinks)
           data(MisNodes)      
    
           output$net <- renderForceNetwork(forceNetwork(
                              Links  = MisLinks, Nodes   = MisNodes,
                              Source = "source", Target  = "target",
                              Value  = "value",  NodeID  = "name",
                              Group  = "group",  opacity = input$opacity,
                              colourScale = JS('force.alpha(1); force.restart(); d3.scaleOrdinal(d3.schemeCategory20);')))
    })
    

    UPDATE (2017.03.20)

    This issue has been resolved in the most recent released version (0.4) of networkD3, and what the OP is asking for is the default behavior.

    0 讨论(0)
提交回复
热议问题