Skip to content

How to avoid endless loop in shiny

4 messages · Doran, Harold, Greg Snow, Michael Peng

#
Hi,

I added two sliderInput into the app with package "shiny": sliderA and
sliderB. The values in the two sliders are correlated. If I change sliderA,
I used updateSliderInput to update the value in sliderB. And also If I
change sliderB, I used  updateSliderInput to update the value in slideA.

The problem is it is an endless loop. How can I use updateSliderInput
without sending message to update the other slider.

Thank.
#
Michael

It is not possible to answer this question without seeing reproducible code. More importantly, it is better to ask Shiny-related questions on stackoverflow using the Shiny tag or on the Shiny google group.



-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Michael Peng
Sent: Tuesday, March 08, 2016 2:36 PM
To: r-help at r-project.org
Subject: [R] How to avoid endless loop in shiny

Hi,

I added two sliderInput into the app with package "shiny": sliderA and sliderB. The values in the two sliders are correlated. If I change sliderA, I used updateSliderInput to update the value in sliderB. And also If I change sliderB, I used  updateSliderInput to update the value in slideA.

The problem is it is an endless loop. How can I use updateSliderInput without sending message to update the other slider.

Thank.


______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
You need to use `isolate` on one of the assignments so that it does
not register as an update.  Here are a few lines of code from the
server.R file for an example that I use that has a slider for r
(correlation) and another slider for r^2 and whenever one is changed,
I want the other to update:

  observe({
    updateSliderInput(session, 'r',
value=isolate(ifelse(input$r<0,-1,1))*sqrt(input$r2))
  })

  observe({
    updateSliderInput(session, 'r2', value=input$r^2)
  })


I did end up in a loop once when I happened to choose just the wrong
value and the rounding caused a jumping back and forth, but all the
other times this has worked perfectly without the endless loop.


On Tue, Mar 8, 2016 at 12:35 PM, Michael Peng
<michael.gang.peng at gmail.com> wrote:

  
    
1 day later
#
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>: