Skip to content
Prev 359209 / 398502 Next

How to avoid endless loop in shiny

Hi Greg,

Isolate may not solve the problem.  For the following code. It runs only
1-2 times and stopped without isolate. If we update the slider with same
value, it will not send a new message.

library("shiny")

ui <- fluidPage(

  titlePanel("Slider Test"),

  sidebarLayout(
    sidebarPanel(
                  min=2, max=10, value=10),

      sliderInput("sliderB", "B:",
                  min = 1, max = 5, value = 5)
      ),

    mainPanel(
      plotOutput("plot")
    )
  )
)

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

  observeEvent(input$sliderA,{updateSliderInput(session, "sliderB", value =
as.integer(input$sliderA/2))})

  observeEvent(input$sliderB,{updateSliderInput(session, "sliderA", value =
isolate(input$sliderB*2))})
}


shinyApp(server = server, ui = ui)

For example:

set B: 4 -> update A: 8 -> update B: 8 (same, no message, end)

changing of A is tricky
If original A is 10, B is 5.

set A: 9  -> update B: 4 (not same, continue) -> update A: 8 (not same,
continue) -> update B: 4 (same, no message, end)


If original A is 8, B is 4.
set A:9 - > update B: 4(same, no message, end)

isolate doesn't work in this case.

Instead, use the following code:

library("shiny")

ui <- fluidPage(

  titlePanel("Slider Test"),

  sidebarLayout(
    sidebarPanel(
                  min=2, max=10, value=10),

      sliderInput("sliderB", "B:",
                  min = 1, max = 5, value = 5)
      ),

    mainPanel(
      plotOutput("plot")
    )
  )
)

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

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

  ignoreNext <- ""

  observeEvent(input$sliderA,{
      if (ignoreNext == "A") {
        ignoreNext <<- ""
      }
      else{
        valB <- as.integer(input$sliderA/2)
        if(valB != input$sliderB){
          ignoreNext <<- "B"
          updateSliderInput(session, "sliderB", value = valB)
        }
      }
    })

  observeEvent(input$sliderB,{
    if (ignoreNext == "B") {
      ignoreNext <<- ""
    }
    else{
      valA <- as.integer(input$sliderA*2)
      if(valA != input$sliderA){
        ignoreNext <<- "A"
        updateSliderInput(session, "sliderA", value = valA)
      }
    }
    })
}


shinyApp(server = server, ui = ui)

2016-03-08 18:00 GMT-05:00 Greg Snow <538280 at gmail.com>: