[R] How to avoid endless loop in shiny

Michael Peng michael.gang.peng at gmail.com
Thu Mar 10 21:27:47 CET 2016


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>:

> 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:
> > 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.
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
>
>
>
> --
> Gregory (Greg) L. Snow Ph.D.
> 538280 at gmail.com
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list