daattali / shinycssloaders

⌛ Add loading animations to a Shiny output while it's recalculating
https://daattali.com/shiny/shinycssloaders-demo/
Other
399 stars 45 forks source link

shinycssloaders won't disappear when using certain chart packages #12

Closed willglanville closed 6 years ago

willglanville commented 6 years ago

Hi -

I am looking to use shinycssloaders on charts from a package I have built with htmlwidgets. The height component of the chart output needs to respond to window size so have implemented a javascript event to update a shiny variable declaring the height. When implementing this, I find the cssloaders do not disappear after the chart has resized.

This same pattern to implement responsiveness works with many other chart packages (normal plots, plotly etc).

I'm able to reproduce the problem with charts from 'dygraphs' another package built with htmlwidgets.

Run the following code, then resize the browser window,

library(dygraphs) library(datasets) library(dygraphs)

shinyApp( ui = shinyUI(fluidPage(

sidebarLayout(
  sidebarPanel(
    tags$head(
      tags$script(HTML('
        $(window).resize(function(event){
        var w = $(this).width();
        var h = $(this).height();
        var obj = {width: w, height: h};
        Shiny.onInputChange("pltChange", obj);
        });
        '))
      ),
    numericInput("months", label = "Months to Predict", 
      value = 72, min = 12, max = 144, step = 12),
    selectInput("interval", label = "Prediction Interval",
      choices = c("0.80", "0.90", "0.95", "0.99"),
      selected = "0.95"),
    checkboxInput("showgrid", label = "Show Grid", value = TRUE)
  ),
  mainPanel(
    uiOutput("dygraphContainer")
  )
)

  )),

server = shinyServer( function(input,output,session){

chartHeight <- reactive({
  if(is.null(input$pltChange$height)) {
    h <- "300px"
  } else {
    h <- paste0(input$pltChange$height-100,"px")
  }

  h

})

output$dygraphContainer <- renderUI({

  shinycssloaders::withSpinner(
    dygraphOutput("dygraph", height=chartHeight()),
    type = 6, 
    color = "#4A4A49"
  )

})

predicted <- reactive({
  hw <- HoltWinters(ldeaths)
  predict(hw, n.ahead = input$months, 
    prediction.interval = TRUE,
    level = as.numeric(input$interval))
})

output$dygraph <- renderDygraph({

  dygraph(predicted(), main = "Predicted Deaths/Month") %>%
    dySeries(c("lwr", "fit", "upr"), label = "Deaths") %>%
    dyOptions(drawGrid = input$showgrid)
})

}) )

andrewsali commented 6 years ago

I think the github version addresses this issue, can you please try:

devtools::install_github("andrewsali/shinycssloaders")

And report back if afterwards it is solved or not?

willglanville commented 6 years ago

Yes, this now behaves! Thanks very much.