如何让forceNetwork {networkD3}以Shiny app为中心?

时间:2017-03-13 13:23:28

标签: r shiny networkd3

在构建显示forceNetwork图表的Shiny应用程序时,网络不会保持居中,但在交互式更改不透明度时会移出视图。

我的问题是,如何更改此行为并使网络保持最初的居中状态?

可重现的例子:

ui.R:

library(shiny)
library(networkD3)

shinyUI(fluidPage( 

      titlePanel("ForceNetD3"), 

      sidebarLayout(
            sidebarPanel(
               sliderInput("opacity",
                      "Opacity",
                       min = 0.1,
                       max = 1,
                       value = 0.4)
      ),
      mainPanel(
            forceNetworkOutput(outputId = "net")
      )
)))

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))
})

图片中的行为:

enter image description here enter image description here Annndd ......它已经消失了:

enter image description here

1 个答案:

答案 0 :(得分:2)

问题是d3.force()中的计时器已用完,并且没有重新加热&#34;何时输入新值。如果不对networkD3进行更改,您可以通过将colourScale = JS('force.alpha(1); force.restart(); d3.scaleOrdinal(d3.schemeCategory20);')添加到forceNetwork()函数中的参数来解决此问题。所以你需要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);')))
})

更新(2017.03.20)

此问题已在networkD3的最新发布版本(0.4)中得到解决,OP要求的是默认行为。