0

I have a small R Shiny app that has two options for setting 4 selectInput fields. They can be selected manually, or through an action button that selects random values taken as a sample of 1 from the available choices in the 4 selectInput fields.

When I run my button with the code below, it works well, but then the choice is overwritten by a different value:

observeEvent(input$button1,  {

    x = sample(Fifa23$Full.Name,1)
    
    updateSelectInput(session, "Nationality",
                      choices = unique(Fifa23$Nationality),
                      selected = Fifa23$Nationality[Fifa23$Full.Name == x])
    
    updateSelectInput(session, "Club",
                      choices = unique(Fifa23$Club[Fifa23$Nationality==input$Nationality]),
                      selected = Fifa23$Club[Fifa23$Full.Name == x])
    
    updateSelectInput(session, "Position",
                      choices = unique(Fifa23$Position[Fifa23$Nationality==input$Nationality & Fifa23$Club==input$Club ]),
                      selected = Fifa23$Positions[Fifa23$Full.Name == x])
    
    updateSelectInput(session, "Name",
                      choices = unique(Fifa23$Full.Name[Fifa23$Nationality==input$Nationality & Fifa23$Club == input$Club & Fifa23$Position == input$Position]),
                      selected = Fifa23$Full.Name[Fifa23$Full.Name==x])    
    
  })

where my observe, for manual selection of selectInput() values as is follows:

observe({
    
    # Can also set the label and select items
    updateSelectInput(session, "Club",
                      choices = unique(Fifa23$Club[Fifa23$Nationality==input$Nationality])
           )
    
  })
  
  observe({
    
    updateSelectInput(session, "Position",
                      choices = unique(Fifa23$Position[Fifa23$Nationality==input$Nationality & Fifa23$Club==input$Club ])
    )
    
  })
  
  observe({
    
    updateSelectInput(session, "Name",
                      choices = unique(Fifa23$Full.Name[Fifa23$Nationality==input$Nationality & Fifa23$Club == input$Club & Fifa23$Position == input$Position])
)
    
  })

thank you for your help in advance.

I tried changing the arguments for Observe() like setting the suspend to TRUE, and the autoDestroy to TRUE, but I'm facing the same issue.

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    You mean the fields are updated on button press, but then are updated automatically soon after, without any user input? Note that when you hit the button, the value of `input$Club` is changed, which will in turn trigger the observe statements. – AdagioMolto Mar 10 '23 at 12:24
  • 2
    Once you change value by the button the reactivty in Observe is triggered because you changed a value that should trigger a change in observe. You change input$Nationality so it has to run all observe that contain it. You have to create a reativeVal that will be true/false based on whether you want to trigger the observe or not. – jyr Mar 10 '23 at 12:26
  • Yes @AdagioMolto this is exactly the problem I am facing, and I am not sure why is it being changed to a new value. So even if it is getting changed based on the value of input$Club, it shouldn't change the randomly selected value of Full.Name – Data Analyst Mar 10 '23 at 12:36
  • Thanks @jyr but can you explain how I should add this reactiveVal? – Data Analyst Mar 10 '23 at 12:37
  • @DataAnalyst you set defaul value to TRUE flag_value <- reactiveVal(TRUE) then at the end of observeEvent(input$button1 you set flat_value(FALSE) and in observe you will have if(flag_value() == TRUE) { do the thing } else { flag_value(TRUE) }, when you press the button observe will be suppressed and flag_value will be reset to default – jyr Mar 10 '23 at 12:42
  • @jyr I updated it as follows, but still facing the same issue: 1) added the below on the top before any observe or observeEvent: flag_value <- reactiveVal(TRUE) 2) added this within each of the observes, before the action : if(flag_value()==TRUE){ 3) added this at the end of each observe: else {flag_value(TRUE)} 4) added this in the end of the observeEvent(input$button1: flag_value(FALSE) – Data Analyst Mar 10 '23 at 12:49
  • @DataAnalyst I should have mentioned that you need a flag for each of the observes as it will get resettled once the observe is activated. – jyr Mar 10 '23 at 13:44
  • 1
    Check out this post and the posts linked within: https://stackoverflow.com/questions/54560439/shiny-update-input-without-reactives-getting-triggered . The method is pretty exactly what @jyr proposes. Can't write a suggestion myself right now. – AdagioMolto Mar 10 '23 at 19:34

0 Answers0