2

Sorry, I don't know if the question is clear enough: In Shiny, every time the slider is sliding, it will only calculate and update the value at the end of the sliding. If I link its value to the chart, it doesn't look very smooth when sliding (the chart will only change when the mouse is released or after seconds, rather than keep changing with the sliding).

Use the slide bar to change the y, and the red point's position in the chart will be changed.

Input and Chart

Part of my code is as follows:

In ui.R:

sliderInput("slider_mean", 
HTML("Try to change the value of ŷ:"),
min = 1, max = 200, value = 100,width="30%"),

plotlyOutput('meanplot'),

In server.R:(This code may not be complete, just to give an example)

  output$meanplot <- renderPlotly({
    
    meantb <- data.frame(y_hat = 1:200) %>%
      mutate(col2 =(y_mean1()-y_hat)^2+(y_mean2()-y_hat)^2+(y_mean3()-y_hat)^2+(y_mean4()-y_hat)^2+(y_mean5()-y_hat)^2+(y_mean6()-y_hat)^2)

    #Here is to input the slider value
    highlight_adjust <- meantb %>% 
      filter(y_hat %in% input$slider_mean)
    
    p=ggplot(meantb,
           aes(x = y_hat, y = col2)) + 
      
      geom_point(size =0.7,color="black") +
      
      geom_point(data=highlight_adjust, 
                 aes(x = y_hat, y = col2), 
                 color='red')+

      geom_line(size = 0.2,color="black") +
    ggplotly(p)
  })

The example from Shiny:

https://shiny.rstudio.com/gallery/slider-bar-and-slider-range.html

If we move the slider bar quickly, the output value will have a delay.

  • I don't know if there is a way, or the shiny server restricts it. I tried to add animation to the slide bar (simulating smooth sliding), but although the animation in the slide bar is smooth, there will still be a delay in the plot (if the animation speed is too fast, it may be until the end of the animation, point in the plot Is still moving) – Stranger6658 Sep 02 '21 at 22:48
  • What I want to achieve is similar to adjusting the volume on a computer. When sliding the volume bar, it changes continuously, rather than changing the volume after releasing the mouse. – Stranger6658 Sep 02 '21 at 22:58
  • 1
    I don't think this is possible. Shiny reactive is not designed to be fast. What you want is front-end -> back-end -> front. In a deeper view, it is user interaction -> javascript -> json data -> web socket -> R json to list -> R input change -> R render function -> json -> web socket -> js. You see how many processes are involved, and neither js and R are designed for its speed. You will always have some lag if you process data from the server. My suggestion is do it purely in front end with javascript and use plotly js APIs to update the chart. In this way you only have js -> plotly API. – lz100 Sep 03 '21 at 00:23
  • Ok, thanks for your idea. Yesterday I also considered js, I will try it – Stranger6658 Sep 03 '21 at 07:19

1 Answers1

2

After reading some of the source code of sliderInput I found out that the default debounce behavior (cf. to the getRatePolicy section of this article) of the slider is only adhered to in absence of a data-immediate attribute in the HTML code.

That is, we just need to add this attribute to the HTML and the slider reacts instantly.

Note. If you look into the source code referenced before, you will see that receiveMessage will reset immediate to false. receiveMessage is called whenever we use updateSliderInput. Thus, after calling updateSliderInput we have to re-assign the immediate data attribute in order to still see the behaviour.

In the example below you see that the values of the hacked slider are updated instantaneously, while the second slider shows default behavior. A click on update will turn of this behavior as sort of collateral damage and in case you want to use updateSliderInput, you should make sure that the data-immediate attribute is added again (the code for the reset button shows one possible way to do so).

Be, however, aware that there is most likely a reason why the shiny team did not expose this functionality to the end user, so this solution should be regarded as a hack and used with care. (Judging from the source code, they use the immediate attribute to overrule the default rate policy when using updateSliderInput(and thus receiveMessage).

library(shiny)
library(shinyjs)

my_sld <- function(...) {
   sld <- sliderInput(...)
   sld$children[[2]]$attribs$`data-immediate` <- "true"
   sld
}

shinyApp(
   fluidPage(
      useShinyjs(),
      my_sld("sld1", "Immediate:", 1, 10, 1), 
      verbatimTextOutput("dbg1"),
      sliderInput("sld2", "Debounced:", 1, 10, 1),
      verbatimTextOutput("dbg2"),
      actionButton("update", "Update kills Immediateness"),
      actionButton("reset", "Re-add immediate attribute")
      ),
   function(input, output, session) {
      output$dbg1 <- renderPrint(input$sld1)
      output$dbg2 <- renderPrint(input$sld2)
      observeEvent(input$update, {
         updateSliderInput(session, "sld1", value = 8)
      })
      observeEvent(input$reset, {
         runjs("$('#sld1').data('immediate', true);")
      })
   })
thothal
  • 16,690
  • 3
  • 36
  • 71