christophergandrud / networkD3

D3 JavaScript Network Graphs from R
http://christophergandrud.github.io/networkD3
652 stars 269 forks source link

Shiny progress bars not working properly with networkD3 #250

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hi, I have some issues with progress bars in Shiny and the package networkD3. If I try to use a progress bar and the networkD3 package, the bar shows up once but not in subsequent calls. But each time the slider is used the progess bar should appear, which is the case without the sankey diagram. I tested in Chrome. Here is a minimum example code:

require(shinyjs)
require(shiny)
require(networkD3)
require(jsonlite)
require(shinydashboard)

ui <- function(){
  fluidPage(
    fluidRow(
      column(width = 12, style = "margin: 0px; padding: 0px;",
             box(title = "Flow Diagram", status = "primary", solidHeader = F, width = 7,
                 sankeyNetworkOutput("sankeyFlow", width = "100%", height = "500px")
             )
      ), 
      sliderInput("numLanes", label = "Number of Top Lanes", min = 0, max = 30, value = 10)
    )
  )
}

server <- function(input, output, session) {

  observeEvent(c(input$numLanes), {
    withProgress(message = "Wait...", value = NULL, {
      Sys.sleep(1.0)
    })
  })

  output$sankeyFlow <- renderSankeyNetwork({
    print("sankey")

    withProgress(message = "Updating flow diagram...", value = NULL, {

      # Load energy projection data
      URL <- "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json"
      Energy <- jsonlite::fromJSON(URL)

      nLanes <- input$numLanes

      plot <- sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
                                  Target = "target", Value = "value", NodeID = "name",
                                  units = "TWh", fontSize = 12, nodeWidth = 30)
      return(plot)
    })
  })
}

shinyApp(ui = ui, server = server)

In my use case I use several progress bars and none are displayed if I include the flow diagram. I do not think it is a Shiny issue as it only occurs with this package. Thanks, Christian

cjyetman commented 5 years ago

Using your example, if I drag the slider around slowly but continually (presumably causing a series of redraws) I do see the progress notifications, which makes me think that the progress notification code must have some kind of timer that only shows the notification when it thinks the process is going to or is taking long enough to warrant the notification. But honestly, I don't know anything about that code... maybe you should ask the shiny developers?

ghost commented 5 years ago

Ok thanks for looking at it. I will cross post as a shiny issue. There is an explicit timer in the progress bar, so it should always render as it is big enough even if it has some timer implemented that only renders on long delays.

cjyetman commented 5 years ago

after some experimentation, I'm pretty sure this is caused by the same issue in #214

there's a PR #215 that would probably fix this, but it hasn't been merged yet

cjyetman commented 5 years ago

issue in Shiny repo is here https://github.com/rstudio/shiny/issues/2329

cjyetman commented 5 years ago

Looks like this is/will be finally fixed upstream with https://github.com/rstudio/shiny/commit/3d117807ac4ffe14942e41edd8e8a5b364dc80a6

will close this issue once I have verified it

cjyetman commented 5 years ago

This issue has been resolved with the dev version of shiny and should be released on CRAN soon as shiny v1.3.3.