0

I am new to Shiny but already find it most interesting fo rapid GUI development.

I am trying to get the following: I basically have two textInputs. When the first is changed, I would like the second one to be automatically updated with some new value (not using any actionButton), and the other way around too. But I am getting an infinite loop.

A simple example where updating means adding one is:

server.r

library(shiny)

shinyServer(function(input, output,session) {

  observe({
    newval = as.numeric(input$field1)+1
    updateTextInput(session,"field2",value=newval)
  })

  observe({
    newval = as.numeric(input$field2)+1
    updateTextInput(session,"field1",value=newval)
  })
})

ui.R

library(shiny)   

shinyUI(basicPage(

    textInput("field1", "Field 1", ""),
    textInput("field2", "Field 2", "")

))

I tried to encapsulate 'input$field1' and 'input$field2' within a call to isolate() but this does not solve the problem.

Any suggestion? Many thanks in advance,

Yvonnick

2
  • What do you expect to happen here - it seems like you want the text fields to just increase forever. Can you clarify how this should work for the user please. Commented Dec 22, 2014 at 18:05
  • Paul, the infinite update loop is exactly what I would like to block, so that a field is updated only once, upon changing the other. Commented Dec 23, 2014 at 8:13

1 Answer 1

2

Just in case someone encounters a similar problem, I finally found the following solution, using boolean reactive values as flags for activating/blocking text input updates.

ui.R

library(shiny)   

shinyUI(basicPage(

  textInput("value1", "Value 1", ""),
  textOutput("value1TRUE"),
  textInput("value2", "Value 2", ""),
  textOutput("value2TRUE")
))

server.R

library(shiny)

shinyServer(function(input, output,session) {

  myVals = reactiveValues(changeValue1=TRUE,changeValue2=TRUE)

  observe({
    if( (input$value1=="") || is.na(input$value1) ) return(NULL)
    isolate({
      if(!myVals$changeValue2) {
        myVals$changeValue2 = TRUE
        return(NULL)
      }
      myVals$changeValue1 = FALSE
      updateTextInput(session,"value2",value=as.numeric(input$value1)+1)
    })
  })

  observe({
    if( (input$value2=="") || is.na(input$value2) ) return(NULL)
    isolate({
      if(!myVals$changeValue1) {
        myVals$changeValue1 = TRUE
        return(NULL)
      }
      myVals$changeValue2 = FALSE
      updateTextInput(session,"value1",value=as.numeric(input$value2)+1)
    })
  })

  output$value1TRUE = renderText(as.character(myVals$changeValue1))
  output$value2TRUE = renderText(as.character(myVals$changeValue2))
})

If some shiny guru finds a lighter solution, I'd be interested to know about it :-)

Best, Yvonnick

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.