rstudio / dygraphs

R interface to dygraphs
http://rstudio.github.io/dygraphs
Other
365 stars 194 forks source link

Problem with zoom sync, rangeSelector and shiny validate #234

Open nFrechen opened 4 years ago

nFrechen commented 4 years ago

I discovered a problem with zoom Synchronisation in combination with shiny::validate:

I have two graphs showing some data with zoom sync. Next I select a different dataset coming from a different timespan. This dataset shall update both graphs, but the second graphs has an error that is handled by shiny::validate. Hence the second graph gets hidden and an error message is displayed instead.

Problem is: the rangeSelector handles are still shown. This is no big problem in itself, but since the new dataset is from a totally different timespan than the original plot, the rangeSelector handles are way off out of the intended frame. This causes the whole app to be scrollable horizontally (see animated gif below). Additionally I can still drag the handlebars (of the invisible second graph) and cause the first graph (that is zoom synced) to scroll to a timespan that is not covered by the data. Strangely the right handlebar seems to get the left one as soon as I start dragging it.

dygraph_zoom_problem

Here is the app I used for this demo:

ui.R

library(dygraphs)

shinyUI(fluidPage(

  titlePanel("Dygraph Zoom Sync"),

  sidebarLayout(
    sidebarPanel(
      selectInput("which", label = "timespan",
                  choices = c("early", "late"))
    ),
    mainPanel(
      wellPanel(dygraphOutput("dygraph", height = "200px")),
      wellPanel(dygraphOutput("dygraph2", height = "200px"))
    )
  )
))

server.R

library(dygraphs)
library(datasets)
library(dplyr)
library(xts)

shinyServer(function(input, output) {

  co2 <- as.xts(co2)

  data <- reactive({
    if(input$which == "early"){
      head(co2, 100)
    }else{
      tail(co2, 100)
    }
  })

  output$dygraph <- renderDygraph({
    dygraph(data(), group = "dyGroup") %>%
      dyRangeSelector()
  })
  output$dygraph2 <- renderDygraph({
    shiny::validate(
      need(input$which=="early", "Fake error")
    )

    dygraph(data(), group = "dyGroup") %>%
      dyRangeSelector()
  })
})

My sessionInfo():

R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS/LAPACK: /usr/lib/libopenblasp-r0.2.19.so

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=C              LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] xts_0.11-2       zoo_1.8-5        dygraphs_1.1.1.6 shiny_1.3.2     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1      lattice_0.20-38 packrat_0.5.0   digest_0.6.18   later_0.8.0     mime_0.6        grid_3.6.0      R6_2.4.0       
 [9] jsonlite_1.6    xtable_1.8-4    magrittr_1.5    rlang_0.3.4     promises_1.0.1  tools_3.6.0     htmlwidgets_1.3 yaml_2.2.0     
[17] httpuv_1.5.1    compiler_3.6.0  htmltools_0.3.6
woodwards commented 4 years ago

I have a similar problem with the zoom handles remaining visible after the conditionalPanel holding the dygraph is hidden.